'Python setuptools installs console scripts even if optional dependency are not installed

i am currently working on a package and am confused with setuptools. This package contains many dependencies and with these dependencies, multiple scripts can be executed via cli.

E.G.

> main_pkg
> main_pkg_which_needs_dep1
> main_pkg_which_needs_dep2
> ...

It is not necessary to have all scripts available on a system. Only the relevant ones. So i thought that i could simply modify my setup.py as follows:

...
entry_points=dict(console_scripts=[
    'main_pkg = main_pkg.main_pkg:main ',
    'main_pkg_which_needs_dep1 = main_pkg.main_pkg:main_dep1 [dep1]',
    ...
]),
... 
extras_require={
    "dep1": ["psycopg"],
    "dep2": ["apsw"],
    "dep3": ["numpy"],
    ...
},

And assummed if someone executes pip install main_pkg, that only main_pkg would be available in CLI. (Therefore, if executing pip install main_pkg[dep1], then there would be main_pkg and main_pkg_which_needs_dep1 available in CLI)

However, executing pip install main_pkg also makes all other console_scripts available through CLI, failing if executing e.g. main_pkg_which_needs_dep1 due to missing dependencies.

Is this behaviour expected by setuptools?

From the documentation i am reading the following:

It is up to the installer to determine how to handle the situation where PDF was not indicated (e.g. omit the console script, provide a warning when attempting to load the entry point, assume the extras are present and let the implementation fail later).

Also, if looking here, the documentation mentions the following:

In this case, the hello-world script is only viable if the pretty-printer extra is indicated, and so a plugin host might exclude that entry point (i.e. not install a console script) if the relevant extra dependencies are not installed.

Am i understanding the documentation correctly, that the installer (plugin host? --> pip?) has to handle this case, which is currently not working?

Or do i have to further modify the setup.py to achieve such a behaviour?

Thanks in advance!



Solution 1:[1]

I ran into this same problem. Based on this thread: https://github.com/pypa/pip/issues/9726, it does not look like you can optionally install console scripts.

However, this comment: https://github.com/pypa/pip/issues/9726#issuecomment-826381705 proposes a solution that may help you. I'll copy-paste it below.

Have myscript with the extra [cli] depends on myscript-cli the package and myscript-cli depends on myscript but contains the entrypoint to the console_script in the main package.

If you install myscript[cli] it requires myscript-cli package which then gets installed and that contains the entrypoint you wanted. This makes myscript[cli] or myscript-cli install both packages, but permits a myscript install that will not require the -cli package and thus will not provide the entrypoint.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Victor Yap