1

I'm building a Python package from a source code repository I have, using a setup.py script with setuptools.setup(...). In this function call I include all the Python libraries needed for the project to be installed using the install_requires argument.

However, I noticed some users do not use all the sub-packages from this package, but only some specific ones, which do not need some libraries that are huge to be installed (e.g., torch).

Given this situation, can I create in the same repository something like myrepo['full'] or myrepo['little']? Do you have any document on how to do so if it's possible?

sinoroc
  • 18,409
  • 2
  • 39
  • 70
Learning from masters
  • 2,032
  • 3
  • 29
  • 42
  • 2
    `extras_require` See https://stackoverflow.com/a/45043494/7976758 and https://setuptools.pypa.io/en/latest/userguide/dependency_management.html#optional-dependencies (switch the docs to `setup.py`). – phd Jan 10 '23 at 13:40
  • if you write it as an answer I'll accept it – Learning from masters Jan 10 '23 at 14:09

1 Answers1

1

This is called optional dependencies and is implemented using extras_require. It's a dictionary mapping names to lists of strings specifying what other distributions must be installed to support those features. For example:

setup(
    name="MyPackage",
    ...,
    extras_require={
        'little': ['dep1', 'dep2'],
        'full': ['dep1', 'dep2', 'torch'],
    },
)

To avoid repeating lists of common dependencies:

common_deps = ['dep1', 'dep2']

setup(
    name="MyPackage",
    ...,
    extras_require={
        'little': common_deps,
        'full': common_deps + ['torch'],
    },
)

See the docs at https://setuptools.pypa.io/en/latest/userguide/dependency_management.html#optional-dependencies. Switch the docs to setup.py in the menu.

phd
  • 82,685
  • 13
  • 120
  • 165