Search code examples
pythonpipsetuptoolssetup.pypypi

How to selective install package in one pypi project which has multiple packages?


I have next folder structure:

.
├── demo
│   └── __init__.py
├── demo2
│   └── __init__.py
└── setup.py

demo/__init__.py:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
def test():
    print("demo1!")
if __name__ == '__main__':
    test()

demo2/__init__.py:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
def test():
    print("demo2!")
if __name__ == '__main__':
    test()

setup.py:

from setuptools import setup, find_packages

setup(
    name="demo20210216",
    version="0.1.0",
    packages=find_packages(),
    entry_points={
        'console_scripts': [
            'demo = demo:test',
            'demo2 = demo2:test',
        ]
    }
)

I use next to upload to pypi:

# python setup.py sdist bdist_wheel
# twine upload dist/*

Then I could use pip install demo20210216 to install the package, and could verify I have two package demo & demo2 installed, meanwhile script demo & demo2 installed in Scripts folder.

Problem:

Everything is fine, just for demo2, most of my audience do not need it. So, I want to find a way: default just install demo1, but if user needed, he/she could use an additional way to install demo2, is there a way to realize it in pip?

(PS: I don't want to separate demo1 and demo2 to different pypi project)


Solution

  • Finally, didn't find any more solutions, just use the solution in comments: that is using one wrapper project depends on other projects (demo1, demo2):

    setup.py:

    from setuptools import setup, find_packages
    
    setup(
        name="demowrapper",
        version="0.1.0",
        packages=find_packages(),
    
        extras_require={
            'demo1': ['demo1'],
            'demo2': ['demo2'],
        },
    )
    

    Then, at least, to user, it looks still could face to one top project demowrapper, and could select the component to install with pip install demowrapper[demo1].