Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python Custom Modules
DeadWither (1)

Python Modules

How to make your own custom module

This is in Python!

So let's make a module in Python!

What is a Module?

A module is a something like a package. Here is an example. Imagine coding in Python is like making something with Lego,
then Modules are boxes you can import and they contain special building blocks, attributes, ones which do not come in the normal lego sets.

This is an example:

I want to print a random integer from 1 to 10 in Python. Without modules, it is impossible to do so. So we use the 'random' module :

import random
print (random.randint(1, 10))

So this is a Module. How do we make one?

These Modules need to be in a separate file and to use them in other py projects, you need to copy the file

When you start, make a new file. We will call this "module_custom.py", now go to "main.py" and do this:

import module_custom

Now go to the "module_custom.py" and write lot's of functions (these will become attributes)! For example, I am only doing a module with 1 attribute.

#This is in "module_custom.py"
def greet() :
 x = input("What is your name?:\n")
 print("Hello" + x)
#How to use them in my project?
In Python, we import the module (we did that when we did the import module_custom) and then when using the attribute, we do this:

See? We put the module name, then a dot, then the attribute name and then the value. It is also the same in the custom

import module_custom

And there you go! A module! When using it in other projects, make a new file and copy paste the code and then import it!

ch1ck3n (2065)

this teaches importing files but not publishing your packages to pypi

DeadWither (1)

@ch1ck3n Importing files, import modules. Should make myself clear. These are to publish as an app in replit