- Sources:
- Python - The Import System REALLY REALLY GOOD & INDEPT TALK
- Handful Guide
- [[Python - import(…)]]
- Python - Importers
- Youtube from ‘Sreekanth’: REALLY good youtube video, explains the sequence for importing a module, looking also at the memory location, and where the module is saved.
- Youtube from ‘SF Python’: it explains the concepts, and show some code behind importlib.machinery, to create your own Finders and Loaders, (together they form an IMPORTER)
- Python - Absolutes and Relative Imports
- [[Python - Understanding builtins.import.canvas|Python - Understanding builtins.import]]
- [[Python - HOW I IMPORT - My init.py Files]]
Summary / Key Points
importstatements search through the list of paths insys.pathsys.pathalways includes the path of the script invoked on the command line and is agnostic to the working directory on the command line.- importing a package is conceptually the same as importing that package’s
__init__.pyfile
Basic Definitions
- module: any
*.pyfile. Its name is the file name. - built-in module: a “module” (written in C) that is compiled into the Python interpreter, and therefore does not have a
*.pyfile. - package: any folder containing a file named
__init__.pyin it. Its name is the name of the folder.- in Python 3.3 and above, any folder (even without a
__init__.pyfile) is considered a package
- in Python 3.3 and above, any folder (even without a
- object: in Python, almost everything is an object - functions, classes, variables, etc.
Example Directory Structure
test/ # root folder
packA/ # package packA
subA/ # subpackage subA
__init__.py
sa1.py
sa2.py
__init__.py
a1.py
a2.py
packB/ # package packB (implicit namespace package)
b1.py
b2.py
math.py
random.py
other.py
start.py
Note that we do not place a __init__.py file in our root test/ folder.
What is an import?
When a module is imported, Python runs all of the code in the module file. When a package is imported, Python runs all of the code in the package’s __init__.py file, if such a file exists. All of the objects defined in the module or the package’s __init__.py file are made available to the importer.
Basics of the Python import and sys.path
According to Python documentation, here is how an import statement searches for the correct module or package to import:
When a module named
spamis imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file namedspam.pyin a list of directories given by the variablesys.path.sys.pathis initialized from these locations:
- The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH(a list of directory names, with the same syntax as the shell variable PATH).- The installation-dependent default.
After initialization, Python programs can modify
sys.path. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. Source: Python 2 and 3
Technically, Python’s documentation is incomplete. The interpreter will not only look for a file (i.e., module) named spam.py, it will also look for a folder (i.e., package) named spam.
Also, Python imports are case-sensitive. import Spam is not the same as import spam.