Module in Python
Python module is a python document that end with the extension .py. A module can define a function, type, variable.... and able to import by another python file.
In python it also have built-in module, example time, that have the function like time.sleep()
Import Module
from [module_name] import [module, function, class0, variable, *] as [alternative_name]Common combination of import:
- import
module_name - from
module_nameimportmodule, function, class0, variable - from
module_nameimport* - import
module_nameasalt_name - from
module_nameimportfunctionasalt_name
Example:
import-example.py
# Import time and using sleep function
import time
time.sleep(1)
# using from + as
from time import sleep as ts
ts(1)
# import everthing from time
from time import *
sleep(1)Custom Module
- It require 2 different python file, one is for the main function execution, another one act as a module.
Usage
custom_module.py
def test_plus(x, y):
return x + ymain_custom_module.py
import custom_module
print(custom_module.test_plus(1, 2))
# or
from custom_module import test_plus
print(test_plus(1, 4))Output:
3
5__main__ variable
For example, today I want to test the module in custom_module.py I will call it inside the module code,
custom_module.py
def test_plus(x, y):
return x + y
print(test_plus(1, 2))Run the code custom_module.py directly:
3main_custom_module.py
from custom_module import test_plusOutput:
3DANGER
The debug code run when import the module. Which is not we expected run on our main code.
The variable
__name__can help us determine whether our code isrunning in the main file itselfrather than being imported as a module.If the function is run on the main file (it's own file),
__name__will return a results of__main__.
custom_module.py
def test_plus(x, y):
return x + y
if __name__ == "__main__":
print(test_plus(1, 2))Run the code custom_module.py directly:
3main_custom_module.py
from custom_module import test_plusOutput:
Now the debug function wasn't executed as the debug code detected that it's not on it's main file.. (the __name__ returned results of custom_module)
__all__ variable
when we import everything from a module it will follow the list of item of __all__, by default it will allow us to import everything we have, through code : from module import *
Without custom __all__ variable.
all_variable_module.py
def f1(x):
return x
def f2(y):
return(y)
def f3(f1, f2):
return f1(f2(1))all-variable.py
from all_variable_module import *
f3(f1(f2(1)))With custom __all__ variable.
- Means that the name inside of the
__all__list are able to import through import everthing(from module import *), if the name is not inside the list of__all__the file will not imported automatically.
all_variable_module.py
__all__ = ["f1", "f2"]
def f1(x):
return x
def f2(y):
return(y)
def f3(f1, f2):
return f1(f2(1))all-variable.py
from all_variable_module import *
f3() # f3 is not definedThe IDE will show the it is not defined: 
However, the IDE will still showing the import hints even custom __all__ variable is defined:

That's means that we still can import it by ourself, but not from *
from all_variable_module import f3
f3()