Search code examples
pythonrepositorysetuptoolspython-packaging

Create different python packages from same repository


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?


Solution

  • 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.