Python scripts from console / command prompt

Each Python package’s “setup.cfg” can make Python scripts callable from any directory, by adding them to your system PATH via the <PythonDistroRoot>/bin directory. On a typical Anaconda Python install, the shortcuts to these scripts are installed in a directory like ~/miniconda3/bin/

Make sure your console_script entry_points are set up correctly before running pip install, or you will get the VersionConflict error (See Notes at bottom of this article). The entry_points can live inside a single file inside the Python module, or several files.

Here is a simple example “src/mypkg/adder.py” that we wish to run directly from console (in any directory) as

add_two 6.75

to get result 8.75. Note that the end user doesn’t even know they’re running a Python script.

#!/usr/bin/env python3
from argparse import ArgumentParser


def add_two(x: float) -> float:
    return x + 2


def cli():
    p = ArgumentParser(description='adds two to number')
    p.add_argument('x', help='number to add two to', type=float)
    P = p.parse_args()

    print(add_two(P.x))

For each function you wish to have be accessible from anywhere on the system be sure there is a function that handles console arguments as in the example above.

Add to setup.cfg like:

[options.entry_points]
console_scripts =
 add_two = mypkg.adder:cli

Notes

If you get VersionConflict error there may be old entry_points in the Python distro bin/ directory that need to be removed.