Skip to content
Sign upLog in
← Back to Community

Python Custom Modules

Profile icon

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 "", now go to "" and do this:

import module_custom

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

#This is in "" 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: *** ```py random.randint(10,50)

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 module_custom.greet()

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!

Profile icon
Profile icon
Profile icon
Profile icon
Profile icon

Is there a way to import a module that is not in the same project?

Profile icon

No. Either publish to pypi or use py import requests but I dont no how to do it of if you can do it

Profile icon

this teaches importing files but not publishing your packages to pypi

Profile icon

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