Skip to content

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

py
from [module_name] import [module, function, class0, variable, *] as [alternative_name]

Common combination of import:

  1. import module_name
  2. from module_name import module, function, class0, variable
  3. from module_name import *
  4. import module_name as alt_name
  5. from module_name import function as alt_name

Example:

import-example.py

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

py
def test_plus(x, y):
    return x + y

main_custom_module.py

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

py
def test_plus(x, y):
    return x + y

print(test_plus(1, 2))

Run the code custom_module.py directly:

3

main_custom_module.py

py
from custom_module import test_plus

Output:

3

DANGER

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 is running in the main file itself rather 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

py
def test_plus(x, y):
    return x + y

if __name__ == "__main__":
    print(test_plus(1, 2))

Run the code custom_module.py directly:

3

main_custom_module.py

py
from custom_module import test_plus

Output:


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

py
def f1(x):
    return x

def f2(y):
    return(y)

def f3(f1, f2):
    return f1(f2(1))

all-variable.py

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

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

py
from all_variable_module import *

f3() # f3 is not defined

The IDE will show the it is not defined: f3 not defined


However, the IDE will still showing the import hints even custom __all__ variable is defined:

import hints

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

py
from all_variable_module import f3

f3()

f3 is defined