Skip to content

jsilva234/understand_python_import_system

Repository files navigation

Explored topics

  1. Basic import Syntax The simplest form of importing a module:

    import module_name
  2. import vs from ... 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
  3. 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
  4. Importing Entire Modules
    Imports the whole module, requiring you to reference its members using dot notation.

    import math
    math.sqrt(4)
  5. Aliasing Imports (as)
    Renaming a module or imported object to simplify code:

    import numpy as np
    from math import sqrt as square_root
  6. Relative Imports
    Importing modules from the same package using relative paths:

    from .sibling_module import ClassName
  7. Absolute Imports
    Using the full path to import modules from the root of the package:

    from package.subpackage import module
  8. Importing All (*)
    Importing everything from a module (not recommended due to namespace pollution):

    from module_name import *
  9. Circular Imports
    Occurs when two modules depend on each other, potentially causing errors. This can be handled using conditional imports or restructuring code.

  10. Module Search Path (sys.path)
    Python searches for modules in directories listed in sys.path. You can modify it to include custom paths:

    import sys
    sys.path.append('/path/to/my/modules')
  11. 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).

  12. Lazy Imports (Python 3.7+)
    Postponing module import until it is actually used (for performance). Can be done manually or using importlib.

  13. Dynamic Imports (importlib) Allows dynamic imports at runtime:

    import importlib
    my_module = importlib.import_module('module_name')
  14. Reloading Modules (importlib.reload) Reloads a module if it has been modified:

    import importlib
    importlib.reload(module_name)
  15. The __all__ Attribute Defines what is imported when from module import * is used. It's a list of public objects the module exports:

    __all__ = ['function_name', 'ClassName']
  16. 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()
  17. Third-Party Libraries and pip Libraries not in the standard library must be installed using package managers like pip:

    pip install requests

    Then import as usual:

    import requests
  18. Namespace Packages (PEP 420) Introduced in Python 3.3+, allows packages without __init__.py. Useful for splitting large packages across multiple directories.

  19. Importing Built-in Modules Python comes with a large standard library of built-in modules:

    import os
    import sys
  20. Import Side Effects Code inside a module runs upon import. Be careful with modules that execute code as a side effect.

  21. Lazy Loading (importlib.util.LazyLoader) Python supports lazy loading with the LazyLoader, which can improve performance for rarely used modules.

  22. Submodules and Subpackages A package can contain submodules and subpackages:

    from package.subpackage import submodule
  23. Environment-specific Imports You can conditionally import modules depending on the environment (OS, Python version):

    import sys
    if sys.platform == 'win32':
        import windows_module
  24. Importing Modules from ZIP Files Python can import modules from ZIP files directly, making distribution easier:

    python -m zipfile -c mypackage.zip mypackage/
  25. Importing with Wildcards and Name Collisions Using * imports everything, but it can cause naming conflicts:

    from module1 import *
    from module2 import *
  26. Platform-specific Imports Import modules depending on the platform:

    if sys.platform == 'darwin':
        import macos_specific_module
  27. 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)
  28. Module Caching (sys.modules) Once a module is imported, it is cached in sys.modules, so future imports are faster. You can manipulate this cache:

    del sys.modules['module_name']
  29. Built-in Module Optimization (C-extensions) Some built-in modules are written in C (like math and datetime) for performance.

  30. Star Imports and Namespace Pollution Using from module import * can clutter the namespace with unwanted names, making it harder to debug.

  31. 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'
  32. Frozen Modules Python can “freeze” modules (compile them into bytecode) for performance or distribution purposes.

  33. Zipimport A built-in mechanism for importing Python files from zip archives, using the zipimport module.

About

Playground to in dept understand of Python import system

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages