Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python setup.py define optional subpackages with optional dependencies

I know that's possible to define optional dependencies in setup.py with extra-require. Now I'm asking myself if its possible to also mark packages as optional in a way that you can chose which subpackages you want to install? Is there a way to map the optional dependencies to the optional packages.

For example if i have a project called project A and this package structure:

Project
   --subPackage 1
   --subPackage 2
   --subPackage 3

I would like to mark subpackage 2 and 3 as optional so that these packages are not installed by default. But if a subpackage is specified via pip or requirement by project B it should be installed with the dependencies.

So the expected behavior would be for project B should be the following:

setup.py for Project B:

    setup(
    name='Project B',
    version='0.0.0',
    install_requires=["ProjectA"])

results in only Project 1 with subpackage 1 is installed. But if i change the install_requires line to install_requires=["ProjectA[Subpackage2]"]. Project A is installed with subpackage 1 and 2 with the given requirements for subpackage 1 and 2.

This there a away to crate a setup.py for Project A to archive this behavior ?

like image 857
Bierbarbar Avatar asked Jul 09 '18 12:07

Bierbarbar


1 Answers

You can do this by splitting your package into namespaced packages instead. In this case, your repo would look something like:

project-subpackage-a/
    setup.py
    project/
        subpackage_a/
            __init__.py

project-subpackage-b/
    setup.py
    project/
        subpackage_b/
            __init__.py

Here, you have two completely separate packages with their own setup.py configurations. This means you can use install_requires and extra_requires just like you would with any other package.

However, unlike other packages, if you were to install both namespaced packages, either separately or through requires, they can both exist within the same package namespace.

That is, if you install both packages, you can then import them as if they came from the same source:

from project import subpackage_a
from project import subpackage_b

For more details on how to set up namespace packages correctly check out the python docs.

like image 96
Ben Horsburgh Avatar answered Oct 11 '22 23:10

Ben Horsburgh