11

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 ?

Bierbarbar
  • 1,399
  • 15
  • 35
  • 3
    You need to split Project A into [namespace packages](https://packaging.python.org/guides/packaging-namespace-packages/). See the [sample](https://github.com/pypa/sample-namespace-packages). – phd Jul 09 '18 at 19:05
  • 1
    I think this does not provide the feature I am looking for. I would want to have one package that i can install "partial" this means only install some of the subpackages. This should be possible by naming the subpackages this in the requierment file... – Bierbarbar Jul 10 '18 at 09:31
  • There is no way to create subpackages without really splitting them into separate packages. – phd Jul 10 '18 at 17:56
  • 4
    What you look for is not possible. Extras only define a list of additional packages to install (in the sense of PyPI packages) and don't define anything besides. There is no such thing as "full" or "partial" package install; a package is an atomic entity. What @phd suggested in the first comment is the correct approach. – hoefling Jul 10 '18 at 22:08

1 Answers1

1

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.

Ben Horsburgh
  • 563
  • 4
  • 10