0

I have implemented a code that install requirements using pip install command without any problem. In order not to install unnecessary packages for the user, it is required to ask them questions (input) and install the packages based on their answers.

Here is my current setup.py file which works perfect but install all packages:

setuptools.setup(
    name="backbone",
    version="1",
    entry_points={
        "console_scripts": [
            'backbone_cli=backbone.backbone:main'
        ]
    },
    author="Mostafa Ghadimi",
    author_email="<my_email>",
    long_description=long_description,
    long_description_content_type="text/markdown",
    install_requires=requirements,
    include_package_data=True,
    url="https://github.com/mostafaghadimi/backbone",
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    packages=setuptools.find_packages(),
    python_requires=">=3.6",
    license='MIT',
)

As an example, let's imagine that we want to ask the user that whether he/she wants to install numpy or not. I have tried does_need_numpy = input('Do you want to install numpy? [y]es/[n]o') but it doesn't work and when I add it to setup.py, face to the following error while I want to install the package (pip install <package_name>):

File "/tmp/pip-req-build-ldgfq6l6/setup.py", line 11, in <module>
        does_need_numpy = input('Do you want to install numpy? [y]es/[n]o')
    EOFError: EOF when reading a line

As I have found, there is some functions like cmdclass install that may be the solution.

Can someone help me how to implement the expected functionality?

Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102
  • 2
    Instead of asking users use [`extras_require`](https://setuptools.pypa.io/en/latest/userguide/dependency_management.html#optional-dependencies) (see also https://stackoverflow.com/search?q=[setuptools]+extras_require) and teach users to install only extras they need. – phd Nov 03 '21 at 13:04
  • @phd Thanks for your answer. Isn't there a way to ask the users an input and install the packages based on their inputs (decisions)? – Mostafa Ghadimi Nov 03 '21 at 13:38
  • 1
    Not with `pip`. It's intended to be used non-interactively. And anyway `pip install ` doesn't use `setup.py`: `setup.py` is used at the build phase but not at the installation phase. – phd Nov 03 '21 at 14:02
  • @phd Thanks for your helpful comments. You have specially helped me figuring the problem out! <3 – Mostafa Ghadimi Nov 10 '21 at 12:31

1 Answers1

0

Thanks from @phd for his comment. As I have found there isn't any way to interactively install requirements based on user input using pip install command. So in order to satisfy the necessities, we have to use extras_require.

The problem was solved after adding extras_require as an argument in the setuptools.setup function.

setuptools.setup(
    name="backbone",
    version="1",
    entry_points={
        "console_scripts": [
            'backbone_cli=backbone.backbone:main'
        ]
    },
    author="Mostafa Ghadimi",
    author_email="<my_email>",
    long_description=long_description,
    long_description_content_type="text/markdown",
    install_requires=requirements,
    include_package_data=True,
    url="https://github.com/mostafaghadimi/backbone",
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
    packages=setuptools.find_packages(),
    python_requires=">=3.6",
    license='MIT',
    extras_require={
        'pandas_numpy': [
            'numpy',
            'pandas_numpy',
        ]
    },
)

And it can be installed locally using the following command in virtual environment.

pip install git+https://$GITLAB_USERNAME:$GITLAB_PASSWORD@$GITLAB_URL[pandas_numpy]

For more information and find out the differences between install_requires and extras_require checkout this link.

Mostafa Ghadimi
  • 5,883
  • 8
  • 64
  • 102