Import Everything from a Python Namespace Package



What are Namespace Packages?

Namespace packages are a special type of package that were introduced in Python 3.3. These allow you to split package contents across multiple directories. If you didn't include at least an empty _init_.py file in your package, then your package becomes a namespace package.

Python supports three main types of namespace packages -

  • Implicit namespace packages
  • pkg_resources-style
  • pkgutil-style

Python Packages and Imports

Python package is a directory containing a _init_.py file and one or more Python modules. When you import from a package, Python looks for modules within that directory structure. For example, if the package structure is like -

mypackage/
    __init__.py
    module1.py
    module2.py

You can import the modules using the following commands -

#To import specific modules
from mypackage import module1
#To import all the modules
from mypackage import *

Importing from Namespace Packages

Importing from namespace packages is similar to importing from regular packages. The following commands can be used to import specific modules and specific functions -

# Import specific modules
from mycompany.tools import tool1
from mycompany.utils import util1

# Import specific functions
from mycompany.tools.tool1 import specific_function

Namespace packages have some considerations when importing everything as they span multiple directories. Some of the techniques to import everything from a Python namespace package are -

Import Individual Components

The best and convenient way is to import everything individually. This helps in maintaining clarity as we are importing and avoiding naming conflicts. The code below demonstrates -

# Import from different parts of the namespace package
from mycompany.tools import tool1, tool2
from mycompany.utils import util1

Use _init_.py Files

Though namespace packages don't require _init_.py files, you can still add them to define what gets imported with *. In each directory of the namespace package, you can add an _init_.py file that defines an _all_ list -

# In mycompany/tools/__init__.py
from . import tool1, tool2
__all__ = ['tool1', 'tool2']

# In mycompany/utils/__init__.py
from . import util1
__all__ = ['util1']

After this, you can use the below command to import everything from specific parts -

from mycompany.tools import *  # Imports tool1 and tool2

Import and Discover

For dynamic cases, you can discover and import all modules programmatically using the following program -

import pkgutil
import importlib
import mycompany

# Import all submodules
for _, name, _ in pkgutil.walk_packages(mycompany.__path__, mycompany.__name__ + '.'):
    importlib.import_module(name)
Updated on: 2025-04-18T15:47:32+05:30

689 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements