TIL35 - Python - Importing Modules/Packages

dhlee91·2020년 9월 20일


목록 보기

오늘은 python modudule과 package를 import하는 법에 대해 알아보자~

What are Modules & Packages?

  • Module - a python file
  • Package - a directory containing multiple modules and files

Modules and packages are files and collections of files that we can import to the current project so that we can use the functions and/or variables defined in those files in our current project.

How do we import Modules?

We import a module by using the import keyword inside current project:

importing the main.py module:
import main

  • when importing a module, we omit the .py file extension

If we want to ipmort only certain functions and/or variables from the module, we can use from... import keywords:

importing say_hello function from main.py:
from main import say_hello

importing say_hello function and var_x variable from main.py:
from main import say_hello, var_x

We can also import all functions and variables from the module with import *. The * notation indicates "all":

from main import *

We can also import the module with a different name to use inside the project using the as keyword:

import module_1 as m1
import module_2 as m2
import module_3 as m3

We can also import the module's specific function with a different name:

from module_1 import func_1 as f1
from module_1 import func_2 as f2

How do we import Packages?

Importing packages is similar to importing modules.

importing mod1.py module located inside pkg package:
import pkg.mod1

importing func1 function from mod1.py module located inside pkg package:
from pkg.mod1 import func1

The __init__.py file

In a package, there is a __init__.py module that gets executed automatically when the package is imported.

If there are modules and functions of the package imported into the __init__.py file first, we don't have to import those modules and functions by writing out its entire path as in the previous examples.

If we first import a specific function inside the package's __init__.py module:

from .mod1 import func1

We can simply the from path as follows:

current project:
from pkg import func1

Also , inside the __init__.py module, we can limit the modules/functions that can be imported by other projects by specifying and designating those that can be imported to the __all__ variable:


from .mod1 import func1
from .mod1 import func2
__all__ = ['func1', 'func2']
  • __all__ variable is a list of strings - the names of modules/functions as strings
  • we can only import the modules/functions designated inside the __all__ variable

current project:

from pkg import func1
from pkg import func2
from pkg import func3 #will result in error --> func3 not in the __all__ variable in the __init__.py file

To use a package created by others, we must first install the package using PIP:

installing the Django package:
pip install Django

After installing the package, we can then import it.

How does Python import Modules & Packages?

When we import a module/package, there is a sequence in which python searches for it:
1. sys.modules
2. built-in modules
3. sys.path

  • sys-modules: once a module is imported, its information is saved to the sys-modules dictionary. Next time we import the same module, python will simply get it from sys-modules
  • built-in modules: these are modules built-in natively in python.
  • sys-path: this a list of paths that the python can run through to look for the modules

Python first searches in sys-modules, then if the module/package is not in there, it will search if it is a built-in module, and if it's not, then it will go through the sys-path. If it can't find the module in sys-path, it will return a ModuleNotFoundError.

sys itself is also a built-in python module, so you can print or edit sys-modules and sys-path:

import sys

Absolute Path vs. Relative Path

So a package is a directory and a module is a file.
When we import a package's module, we must write out the file's path so that python can find it.

We can write this path as absolute path or as relative path.

  • Absolute path: The entire path of the file starting from the root directory. The current project's directory is the root directory, and thus the starting point will be inside this current project directory, so we can omit this directory when naming the path, but must write out all the directories leading to the importing module from this point. As long as inside the current project directory, no matter in which sub directories or modules, the absolute path will be the absolute path that doesn't change

    from pkg.subpkg.subsubpkg1.mod1 import func1

  • Relative Path: The path starting from the location of the importing module. The starting point is not the current project directory, but the directory of the importing module. If you have to access a module from a different directory, you will use the .. notation, which denotes "the directory above current directory":

    inside subsubpkg.mod1:
    from ..subsubpkg1.subsubpkg2.mod1 import func1
    > ..subsubpkg1 indicates the subpkg directory, inside which are the subsubpkg1 and subsubpkg2 directories

When using relative path, we must be aware of the current directory and if the importing module is located in another directory, we must be sure to correctly reference the path by using appropriate notations.

0개의 댓글