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.

Btibert3
  • 38,798
  • 44
  • 129
  • 168
  • Something like [this](https://stackoverflow.com/questions/5879869/run-python-script-without-the-python-keyword)? – Tomerikoo Mar 07 '21 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 – Btibert3 Mar 07 '21 at 20:28
  • I couldn't find a duplicate (and don't have the technical experience to write an answer), but I believe [this](https://packaging.python.org/guides/installing-stand-alone-command-line-tools/) is exactly what you're looking for. – Tomerikoo Mar 07 '21 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) – astrochun Mar 07 '21 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 – Btibert3 Mar 07 '21 at 20:43
  • Do you have a `setup.py` in your repository? – astrochun Mar 07 '21 at 20:47
  • My package can, yes. I have a number of packages, I am trying to understand how to enable this generally. I have been using flit lately which avoids the setup file, but I can always revert. It is simply the presence of a setup.py file, or is it more to it than that? – Btibert3 Mar 07 '21 at 21:04
  • @Btibert3 hopefully the below instructions is helpful to you. – astrochun Mar 07 '21 at 23:15

2 Answers2

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.

Czaporka
  • 2,190
  • 3
  • 10
  • 23
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.

astrochun
  • 1,642
  • 2
  • 7
  • 18