6

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?

tryingtosolve
  • 763
  • 5
  • 20
  • If `subpackage_a` and `subpackage_b` are independent why make them subpackages and not independent libraries? – phd Oct 07 '18 at 21:48

1 Answers1

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"],
)
korommatyi
  • 123
  • 5
  • 1
    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. – Will Udstrand Jul 18 '22 at 21:55
  • Would this help? https://stackoverflow.com/a/65902365/4350939 – korommatyi May 26 '23 at 14:51