2

I have seen a few examples of python packages that, after installing from something like pypi, have CLIs associated with the tooling.

Two examples: rasa (e.g. rasa init) or streamlit (e.g. streamlit hello).

I am interested in exploring this for my own packages, with my requirement that I do not want to preface my commands with python. For example, rasa init as shown above, not python rasa init, but admittedly I have no idea how this is happening under the hood.

8
  • Something like this? Commented Mar 7, 2021 at 20:09
  • Doesn't the shebang only apply to *nix? If a windows user installed from pypi, would that still work and does it totally remove the need for the python prefix? I don't know if it matters, but my question centers on the pip install of the package, and then it just works for the user, regardless of OS Commented Mar 7, 2021 at 20:28
  • I couldn't find a duplicate (and don't have the technical experience to write an answer), but I believe this is exactly what you're looking for. Commented Mar 7, 2021 at 20:37
  • Question: Are you trying to fully package it for pip installation? If so then you would want to specify the scripts as part of the package. The shebang is the start of making the script. Make sure the file is executable (e.g., chmod +x with git-bash) Commented Mar 7, 2021 at 20:39
  • Yes, my requirement that this ability occurs for a user after a pip installation. The examples of rasa and streamlit above enable this functionality, it's just not obvious to me as to how it actually takes place Commented Mar 7, 2021 at 20:43

2 Answers 2

4

entry_points or scripts are used for this.

Here's the relevant piece of streamlit's setup.py:

setuptools.setup(
    ...
    entry_points={"console_scripts": ["streamlit = streamlit.cli:main"]},
    ...
    # For Windows so that streamlit * commands work ie.
    # - streamlit version
    # - streamlit hello
    scripts=["bin/streamlit.cmd"],
)

Interestingly, some sources seem to claim that entry_points doesn't work on Windows, while others claim that it is scripts that doesn't work on Windows. Personally I've always been using entry_points and it works for me both on Windows 10 and on Linux.

Sign up to request clarification or add additional context in comments.

Comments

1

This guide may be of help

Here's one of my simple setup.py to illustrate how I did it.

The scripts= option allow you to specify where the script(s) are. When you install it, then these scripts will be included in the paths allowing you to run them.

The scripts should be:

  1. Executable chmod +x
  2. Have a python shebang #!/usr/bin/env python
  3. It is also best practices to have a __name__ == __main__ part of the code

To test it, you can build it and install it in a virtualenv

python setup.py sdist bdist_wheel  # This will build the tar.gz (identical to what pypi uses)
virtualenv venv  # Create an env so you don't mess your setups
source venv/bin/activate  # Activate environment
pip install -e dist/<package_name>-<version>.tar.gz  # This installs it in the venv

pip list should show it's installed

If installation is successful your script should be available. On Mac, it's which script_name. The path will point to the venv.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.