How do I handle namespace packages that will serve as dependencies for another package
Asked Answered
I

1

8

I have written three Python modules, two of them are independent but the last one would depend on the two independent modules. For example, consider the following structure

myProject/
    subpackage_a/
        __init__.py
        ...
    subpackage_b/
        __init__.py
        ...
    mainpackage/
        __init__.py
        ...

mainpackage depends on subpackage_a and subpackage_b, where as subpackage_a and subpackage_b can be used independently. In other words, in mainpackage, there are references to subpackage_a and subpackage_b. In a nutshell, I want the user to be able to do from myProject import subpackage_a and start doing subpackage_a.subfunction(args) without calling mainpackage. I also want the user to use from myProject import mainpackage and start using mainpackage.mainfucntion(args), where mainpackage.mainfucntion will call the functions in the subpackages.

I learned about namespace packaging. However, I couldn't find anything on namespace packages that involve dependencies. I don't have to use namespace packaging if there's a better solution. Can I get some suggestions on what I should look for?

Intersect answered 5/10, 2018 at 21:27 Comment(1)
If subpackage_a and subpackage_b are independent why make them subpackages and not independent libraries?Beatrix
S
0

You need to specify a name for your packages in the respective setup.py anyway and you can simply use those names when defining your dependencies. Here is a slightly simplified example with only one sub-package:

myProject
  sub/
    setup.py
    myProject/
      sub/
        __init__.py
  main/
    setup.py
    myProject/
      main/
        __init__.py

And you setups files should look like this:

myProject/sub/setup.py

from setuptools import setup, find_namespace_packages

setup(
    name="my-project-sub",
    ...
    packages=find_namespace_packages(include=["myProject.*"]),
)

myProject/main/setup.py

from setuptools import setup, find_namespace_packages

setup(
    name="my-project-main",
    ...
    packages=find_namespace_packages(include=["myProject.*"]),
    install_requires=["my-project-sub"],
)
Sweatt answered 15/12, 2021 at 14:50 Comment(2)
Is there a way to have it install the required sub-package on its own when installing the main package? I am getting a ERROR: No matching distribution found for my-project-sub when I try to install the main package first.Dyewood
Would this help? https://mcmap.net/q/245088/-how-to-include-and-install-local-dependencies-in-setup-py-in-pythonSweatt

© 2022 - 2024 — McMap. All rights reserved.