3. Importing modules

3.1. Importing modules

3.1.1. Import module

  • import ...

3.1.1.1. Syntax

import module
import module.submodule

3.1.1.2. Example

import random

3.1.2. Importing function from module

  • from ... import ...

3.1.2.1. Syntax

from module import function
from module.submodule import function

3.1.2.2. Example

from random import randint

3.1.3. Import and alias

  • import ... as ...

3.1.3.1. Syntax

import module as alias
from module import function as alias

3.1.3.2. Example

import numpy as np
from django.utils.translation import ugettext_lazy as _

3.1.4. Relative imports

  • from . import ...

  • from .. import ...

3.1.4.1. Syntax

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

3.2. 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 *

3.2.1. Python file is a module

game.py

3.2.2. Directory with __init__.py file

game
    __init__.py

3.2.3. Importing from own modules

from game import run

3.3. Example

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

3.3.1. Importing variable or constant from module

from game.config import RESOLUTION_X
from game.config import RESOLUTION_Y
Listing 101. Preferred
from game.config import RESOLUTION_X, RESOLUTION_Y

3.3.2. Importing submodules

from game.dragon import red
from game.dragon import white


my_dragon1 = red.RedDragon()
my_dragon2 = white.WhiteDragon()
from game.dragon import red, white

my_dragon1 = red.RedDragon()
my_dragon2 = white.WhiteDragon()

3.3.3. Importing all

from game.dragon import *

my_dragon1 = red.RedDragon()
my_dragon2 = white.WhiteDragon()

3.3.4. Importing objects from modules

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

my_dragon1 = RedDragon()
my_dragon2 = WhiteDragon()

3.3.5. Importing with aliases

from game.dragon.red import RedDragon as Smok

wawelski = Smok()

3.4. Import path

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

import sys

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

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