-
Basic import Syntax The simplest form of importing a module:
import module_name
-
import
vsfrom ... import ...
Importing a module vs importing specific attributes (functions, classes, variables) from a module:import math # Import the entire math module from math import pi # Import only the 'pi' constant
-
Importing Specific Functions, Classes, or Variables You can import specific parts of a module to avoid importing everything:
from module_name import function_name, ClassName
-
Importing Entire Modules
Imports the whole module, requiring you to reference its members using dot notation.import math math.sqrt(4)
-
Aliasing Imports (as)
Renaming a module or imported object to simplify code:import numpy as np from math import sqrt as square_root
-
Relative Imports
Importing modules from the same package using relative paths:from .sibling_module import ClassName
-
Absolute Imports
Using the full path to import modules from the root of the package:from package.subpackage import module
-
Importing All (
*
)
Importing everything from a module (not recommended due to namespace pollution):from module_name import *
-
Circular Imports
Occurs when two modules depend on each other, potentially causing errors. This can be handled using conditional imports or restructuring code. -
Module Search Path (
sys.path
)
Python searches for modules in directories listed insys.path
. You can modify it to include custom paths:import sys sys.path.append('/path/to/my/modules')
-
The
__init__.py
File
Defines a package and can control package-level imports. Required for treating a directory as a package (before Python 3.3). -
Lazy Imports (Python 3.7+)
Postponing module import until it is actually used (for performance). Can be done manually or usingimportlib
. -
Dynamic Imports (
importlib
) Allows dynamic imports at runtime:import importlib my_module = importlib.import_module('module_name')
-
Reloading Modules (
importlib.reload
) Reloads a module if it has been modified:import importlib importlib.reload(module_name)
-
The
__all__
Attribute Defines what is imported whenfrom module import *
is used. It's a list of public objects the module exports:__all__ = ['function_name', 'ClassName']
-
Custom Modules You can create your own Python modules (just .py files) and import them as needed:
# file: mymodule.py def greet(): print("Hello!") # Usage in another file import mymodule mymodule.greet()
-
Third-Party Libraries and
pip
Libraries not in the standard library must be installed using package managers likepip
:pip install requests
Then import as usual:
import requests
-
Namespace Packages (PEP 420) Introduced in Python 3.3+, allows packages without
__init__.py
. Useful for splitting large packages across multiple directories. -
Importing Built-in Modules Python comes with a large standard library of built-in modules:
import os import sys
-
Import Side Effects Code inside a module runs upon import. Be careful with modules that execute code as a side effect.
-
Lazy Loading (
importlib.util.LazyLoader
) Python supports lazy loading with theLazyLoader
, which can improve performance for rarely used modules. -
Submodules and Subpackages A package can contain submodules and subpackages:
from package.subpackage import submodule
-
Environment-specific Imports You can conditionally import modules depending on the environment (OS, Python version):
import sys if sys.platform == 'win32': import windows_module
-
Importing Modules from ZIP Files Python can import modules from ZIP files directly, making distribution easier:
python -m zipfile -c mypackage.zip mypackage/
-
Importing with Wildcards and Name Collisions Using
*
imports everything, but it can cause naming conflicts:from module1 import * from module2 import *
-
Platform-specific Imports Import modules depending on the platform:
if sys.platform == 'darwin': import macos_specific_module
-
Import Hooks Custom hooks that alter the import system behavior, typically used for logging, profiling, or custom module loading:
import sys sys.meta_path.append(my_custom_import_hook)
-
Module Caching (
sys.modules
) Once a module is imported, it is cached insys.modules
, so future imports are faster. You can manipulate this cache:del sys.modules['module_name']
-
Built-in Module Optimization (C-extensions) Some built-in modules are written in C (like
math
anddatetime
) for performance. -
Star Imports and Namespace Pollution Using
from module import *
can clutter the namespace with unwanted names, making it harder to debug. -
PEP 562 — Module-level
__getattr__
Starting from Python 3.7, you can define a module-level__getattr__
to control attribute access dynamically:def __getattr__(name): if name == 'dynamic_attr': return 'Hello'
-
Frozen Modules Python can “freeze” modules (compile them into bytecode) for performance or distribution purposes.
-
Zipimport A built-in mechanism for importing Python files from zip archives, using the
zipimport
module.
-
Notifications
You must be signed in to change notification settings - Fork 0
jsilva234/understand_python_import_system
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
Playground to in dept understand of Python import system
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published