Learn to Code via Tutorials on Repl.it!

← Back to all posts
Python Custom Modules
DeadWither

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 :



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:



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.



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



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!

Voters
DeadWither
Comments
hotnewtop
lsommerer

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

DeadWither

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

ch1ck3n

this teaches importing files but not publishing your packages to pypi

DeadWither

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