18.2. Module Importï
18.2.1. Importing modulesï
18.2.2. Import moduleï
import ...
Syntax:
import module
import module.submodule
Example:
import random
18.2.3. Importing function from moduleï
from ... import ...
Syntax:
from module import function
from module.submodule import function
Example:
from random import randint
18.2.4. Import and aliasï
import ... as ...
Syntax:
import module as alias
from module import function as alias
Example:
import numpy as np
from django.utils.translation import gettext_lazy as _
18.2.5. Relative importsï
from . import ...from .. import ...
Syntax:
from . import module
from .. import module
from .module import function
from ..module import function
18.2.6. What is Python Moduleï
Every Python file is a module
Every directory with
__init__.pyfile is a modulePython does not recognize whether it is a file or dir with init
Useful when you start simple, and then expand
Usually
__init__.pyis emptyIf you define
__all__: list[str]in__init__.pyit will import only those functions whenfrom MODULE import *
18.2.7. Python file is a moduleï
game.py
18.2.8. Directory with __init__.py fileï
game
__init__.py
18.2.9. Importing from own modulesï
from game import run
18.2.10. Examplesï
game
__init__.py
config.py
api.py
dragon
__init__.py
wawelski.py
red.py
black.py
white.py
18.2.11. Importing variable or constant from moduleï
from game.config import RESOLUTION_X
from game.config import RESOLUTION_Y
Preferred:
from game.config import RESOLUTION_X, RESOLUTION_Y
18.2.12. Importing submodulesï
from game.dragon import red
from game.dragon import white
dragon1 = red.RedDragon()
dragon2 = white.WhiteDragon()
from game.dragon import red, white
dragon1 = red.RedDragon()
dragon2 = white.WhiteDragon()
18.2.13. Importing allï
from game.dragon import *
dragon1 = red.RedDragon()
dragon2 = white.WhiteDragon()
18.2.14. Importing objects from modulesï
from game.dragon.red import RedDragon
from game.dragon.white import WhiteDragon
dragon1 = RedDragon()
dragon2 = WhiteDragon()
18.2.15. Importing with aliasesï
from game.dragon.red import RedDragon as Smok
dragon = Smok()
18.2.16. Import pathï
Watch-out module names which are the same as in stdlib
import sys
sys.path
# ['',
# '/opt/homebrew/Cellar/python@3.13/3.13.3_1/Frameworks/Python.framework/Versions/3.13/lib/python313.zip',
# '/opt/homebrew/Cellar/python@3.13/3.13.3_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13',
# '/opt/homebrew/Cellar/python@3.13/3.13.3_1/Frameworks/Python.framework/Versions/3.13/lib/python3.13/lib-dynload',
# '/opt/homebrew/lib/python3.13/site-packages']
sys.path.append('/path/to/directory')
sys.path.insert(0, '/path/to/directory')
18.2.17. __name__ï
Zmienna
__name__pozwala ustaliÄ czy dany plik jest wykonywany czy importowany.Jeżeli dany plik jest wykonywany, zmienna
__name__ustawiana jest na'__main__'.Jeżeli dany plik jest importowany jako moduÅ, zmienna
__name__ustawiana jest na nazwÄ moduÅu.Jest to przydatne na przykÅad przy testowaniu moduÅów.
18.2.18. Example 1ï
Wypisane na konsoli zostanie
'hello world!'jeżeli dany plik jest uruchamiany z konsoli.Powyższy kod nie wykona siÄ natomiast jeżeli plik zaimportujemy jako moduÅ w innym pliku.
if __name__ == '__main__':
print('hello world')
18.2.19. Example 2ï
Jeżeli skrypt wywoÅywany jest z konsoli "z rÄki" to uruchom funkcjÄ
run()Jeżeli zostaÅ zaimportowany, to ten fragment bÄdzie zignorowany
I trzeba uruchomiÄ funkcjÄ
run()samodzielnie - kontrolowanie
def run():
...
if __name__ == '__main__':
run()
18.2.20. Exampleï
import logging
log = logging.getLogger(__name__)