1.1. Module Import

1.1.1. Importing modules

1.1.2. Import module

  • import ...

Syntax:

import module
import module.submodule

Example:

import random

1.1.3. Importing function from module

  • from ... import ...

Syntax:

from module import function
from module.submodule import function

Example:

from random import randint

1.1.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 _

1.1.5. Relative imports

  • from . import ...

  • from .. import ...

Syntax:

from . import module
from .. import module
from .module import function
from ..module import function

1.1.6. What is Python Module

  • Every Python file is a module

  • Every directory with __init__.py file is a module

  • Python does not recognize whether it is a file or dir with init

  • Useful when you start simple, and then expand

  • Usually __init__.py is empty

  • If you define __all__: list[str] in __init__.py it will import only those functions when from MODULE import *

1.1.7. Python file is a module

game.py

1.1.8. Directory with __init__.py file

game
    __init__.py

1.1.9. Importing from own modules

from game import run

1.1.10. Examples

game
    __init__.py
    config.py
    api.py
    dragon
        __init__.py
        wawelski.py
        red.py
        black.py
        white.py

1.1.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

1.1.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()

1.1.13. Importing all

from game.dragon import *

dragon1 = red.RedDragon()
dragon2 = white.WhiteDragon()

1.1.14. Importing objects from modules

from game.dragon.red import RedDragon
from game.dragon.white import WhiteDragon

dragon1 = RedDragon()
dragon2 = WhiteDragon()

1.1.15. Importing with aliases

from game.dragon.red import RedDragon as Smok

dragon = Smok()

1.1.16. Import path

  • Watch-out module names which are the same as in stdlib

import sys

sys.path
# ['/Applications/PyCharm 2019.2 EAP.app/Contents/helpers/pydev',
#  '/Users/Developer/my_project',
#  '/Applications/PyCharm 2019.2 EAP.app/Contents/helpers/pycharm_display',
#  '/Applications/PyCharm 2019.2 EAP.app/Contents/helpers/third_party/thriftpy',
#  '/Applications/PyCharm 2019.2 EAP.app/Contents/helpers/pydev',
#  '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python37.zip',
#  '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7',
#  '/usr/local/Cellar/python/3.7.4/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload',
#  '/Users/matt/Developer/book-python/.venv-3.7.3/lib/python3.7/site-packages',
#  '/Applications/PyCharm 2019.2 EAP.app/Contents/helpers/pycharm_matplotlib_backend']

sys.path.append('/path/to/directory')
sys.path.insert(0, '/path/to/directory')

1.1.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.

1.1.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')

1.1.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()

1.1.20. Example

import logging

log = logging.getLogger(__name__)