Search code examples
pythonpython-importpython-packaging

How to install local python modules in subdirectories as separate packages from the root directory?


I have a repo with this structure:

root_dir/
├── python/
│   └── libraries/
│       └──types/
│           ├── type1/
│           │   └── math/
│           │       └── math.py
│           └── type2/
│               └── categories/
│                   └── category1/ 
│                       ├── module1/
│                       │   └── module1.py
│                       ├── module2/
│                       │   ├──module2.py
│                       │   └── test/
│                       │       └── test_module2.py
│                       └── module3/
│                           ├── module3.py
│                           └── test/
│                               └── test_module3.py
└── notebooks/
    └── type1/
        └── categories/
            └── category1/
                └── notebook.ipynb
            

How can I install root_dir\python\libraries\types\type1 as types.type1 and root_dir\python\libraries\types\type2\categories as types.type2.categories from the root directory root_dir as two separate packages? I want to be able to import these modules to notebook.ipynb using:

from types.type1.math.math import Vector
from types.type2.categories.category1.module1.module1 import Calculator
from types.type2.categories.category1.module2.module2 import Processor

Solution

  • Adding a setup.py file to root_dir\python\libraries like this:

    from setuptools import setup, find_namespace_packages
    
    setup(
        name='types.type1',
        version='0.1',
        packages=find_namespace_packages(where='types', include=['type1.math']),
        package_dir={'': 'types'},
    )
    

    and a second setup.py file to root_dir\python\libraries\types like this:

    from setuptools import setup, find_namespace_packages
    
    setup(
        name='types.type2.categories',
        version='0.1',
        packages=find_namespace_packages(where='type2'),
        package_dir={'': 'type2'},
        install_requires=[
            'types.type1',
        ]
    )
    

    has solved the problem. I can now use these import statements:

    from type1.math.math import Vector
    from categories.category1.module1.module1 import Calculator
    from categories.category1.module2.module2 import Processor
    

    which works fine for my project.