You can use the setuptools library to help package your application into a wheel (installable module, like from PyPi), then add the scripts argument to specify how you want to run it from the command line:

setup(
    ...
    scripts=['bin/funniest-joke'],
    ...
)

When we install the package, setuptools will copy the script to our PATH and make it available for general use

You can find more information here about how to incorporate this functionality.

Answer from Xiddoc on Stack Overflow
🌐
Python Packaging
packaging.python.org › en › latest › guides › creating-command-line-tools
Creating and packaging command-line tools - Python Packaging User Guide
Now, add an empty __init__.py file, to define the project as a regular import package. The file __main__.py marks the main entry point for the application when running it via runpy (i.e. python -m greetings, which works immediately with flat layout, but requires installation of the package with src layout), so initialize the command-line interface here:
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › scenarios › cli
Command-line Applications — The Hitchhiker's Guide to Python
Cliff is a framework for building command-line programs. It uses setuptools entry points to provide subcommands, output formatters, and other extensions. The framework is meant to be used to create multi-level commands such as svn and git, where the main program handles some basic argument parsing and then invokes a sub-command to do the work. Cement is an advanced CLI Application ...
🌐
Real Python
realpython.com › python-typer-cli
Build a Command-Line To-Do App With Python and Typer – Real Python
July 3, 2026 - You can download the entire code and all the additional resources for this to-do CLI application by clicking the link below and going to the source_code_final/ directory: Get Source Code: Click here to get the source code you’ll use to build a to-do app for your command line using Python and Typer. In this step-by-step project, you’ll build a command-line interface (CLI) application to manage a to-do list.
🌐
Opensource.com
opensource.com › article › 22 › 7 › bootstrap-python-command-line-application
Turn your Python script into a command-line application | Opensource.com
With scaffold and click in Python, you can level up even a simple utility into a full-fledged command-line interface tool.
🌐
DEV Community
dev.to › wangonya › building-command-line-applications-with-python-5l4
Building Command-Line Applications with Python - DEV Community
August 16, 2019 - When I started teaching myself Python last year, one of the most interesting things I discovered was how relatively easy it was to create CLI apps. Since I learn best by doing, I went ahead to create norris-cli among other apps, and discovered the possibilities were endless 😄. Building these kinds of projects is a great way to learn, and it's super fun.
🌐
Thomas Stringer
trstringer.com › easy-and-nice-python-cli
The easy (and nice) way to do CLI apps in Python | Thomas Stringer
October 20, 2016 - The best way to install (and uninstall) your Python CLI app is to use pip (pip3 for Python 3). In the root directory of the CLI source code, running pip3 install . will install this app using setup.py as “instructions”. Likewise, running pip3 uninstall pycli will remove the app.
🌐
KDnuggets
kdnuggets.com › build-a-command-line-app-with-python-in-7-easy-steps
Build a Command-Line App with Python in 7 Easy Steps - KDnuggets
You don't need any third-party libraries for this project. So only make sure you’re using a recent version of Python. This tutorial uses Python 3.11. In the todo.py file, start by importing the required modules. For our simple to-do list app, we'll need the following: ... Recall that we’ll use command-line flags to add, list, and remove tasks.
Find elsewhere
🌐
Honeybadger
honeybadger.io › blog › building-command-line-applications-in-python-a-comprehensive-guide
Building command-line applications in Python - Honeybadger Developer Blog
March 19, 2024 - Hence, flags are used in a command line application to turn specific features on or off. Usually, we execute a Python program from the command-line terminal using the syntax python filename.py or python3 filename.py.
🌐
Better Programming
betterprogramming.pub › python-click-building-your-first-command-line-interface-application-6947d5319ef7
Create Your First CLI Application With Python Click | by mugoh mwaura | Better Programming
August 6, 2020 - So we give our application a spin command where we can specify the spinning speed. python spin_world_v3.py time 0900 1700 --speed 1500m/h · This has given us a simple understanding of the CLI commands concept. We’ve looked at how command-line tools work and the basics of creating a command-line interface.
Top answer
1 of 1
8

I think you may be confusing two things. Having a src directory in your source distribution is a reasonably common, idiomatic thing to do; having that in the installed package or app, not so much.

There are two basic ways to structure this.


First, you can build a Python distribution that installs a package into site-packages, and installs a script into wherever Python scripts go on the PATH. The Python Packaging User Guide covers this in its tutorial on building and distributing packages. See the Layout and Scripts sections, and the samples linked from there.

This will usually give you an installed layout something like this:

<...somewhere system/user/venv .../lib/pythonX.Y/site-packages>
    app_name/
        file1.py
        file2.py
        cli.py
        __init__.py

<...somewhere.../bin/>
    app_name

But, depending on how the user has chosen to install things, it could instead be an egg, or a zipped package, or a wheel, or anything else. You don't care, as long as your code works. In particular, your code can assume that app_name is an importable package.

Ideally, the app_name script on your path is an "entry-point" script (pip itself may be a good example on your system), ideally one built on the fly at install time. With setuptools, You can just specify which package it should import and which function it should call in that package, it will do everything else—making sure to shebang the Python actually used at install time, figuring out how to pkg_resources up the package and add it to the sys.path (not needed by default, but if you don't want it to be importable, you can make that work), and so on.

As mentioned in a comment from the original asker, python-commandline-bootstrap might help you put this solution together more quickly.


The alternative is to keep everything out of site-packages, and make the package specific to your app. In this case, what you basically want to do is:

  • Install a directory that contains both the package (either as a directory, or zipped up) and the wrapper script.
  • In the wrapper script, add dirname(abspath(argv[0])) to sys.path before the import.
  • Symlink the wrapper script into some place on the user's PATH.

In this case, you have to write the wrapper script manually, but then you don't need anything fancy this way.

But often, you don't really want an application to depend on the user having some specific version and setup of Python. You may want to use a tool like pyInstaller, py2exe, py2app, cx_Freeze, zc.buildout, etc. that does all of the above and more. They all do different things, all the way up to the extreme of building a Mac .app bundle directory with a custom, standalone, stripped Python framework and stdlib and a wrapper executable that embeds the framework.


Either way, you really don't want to call the package directory src. That means the package itself will be named src, which is not a great name. If your app is called spamifier, you want to see spamifier in your tracebacks, not src, right?

Author: AnthonyBloomer
🌐
Medium
medium.com › @zahedialfurquan20 › building-a-command-line-application-using-python-fbcf7a60863a
Building a command line application using python, typer and poetry | by Alfurquan Zahedi | Medium
March 24, 2024 - In this article, we will build a functional expense-tracker application for the command line using Python, use Poetry for package management , and Typer which is a relatively young library for creating powerful command-line interface (CLI) applications in almost no time.
🌐
pytz
pythonhosted.org › pyCLI
cli — command line tools — pyCLI devel documentation
The cli package is a framework for making simple, correct command line applications in Python. With cli, you can quickly add standard command line parsing; logging; unit and functional testing; and profiling to your CLI apps. To make it easier to do the right thing, cli wraps all of these tools ...
🌐
Medium
medium.com › @nabulovivian2014 › how-i-created-a-command-line-application-with-python-edf332b8d414
How I created a command line application with python | by nabulo vivian | Medium
September 20, 2018 - The virtual environment is achieved through running a pip install virtualenv from your projects directory using the command prompt terminal. Virtual environment after being installed, create a venv module by running, “virtualenv venv” · Since am using pyinquirer, Install it in the virtual environment, venv using pip install pyinquirer from the scripts folder of the virtual environment. ... create a python file ‘cli_newsapi.py’ from where you are going to implement your command line application.
🌐
GitConnected
levelup.gitconnected.com › python-command-line-project-architecture-design-2c0adfed4da8
Python Command-Line Project — Architecture Design | by James K. Ralph | Level Up Coding
April 30, 2020 - That directory going to be called when we run our python program. We may not want to keep our business logic separated from this section — as this could serve as our main playground. This may also contain argument parsing, conditional logic, and exit strategies. We added app/lib, which will host all scripts that directly is not serving as a business outcome but helps the developer to re-use across the project.
🌐
Vanilla-project
vanilla-project.guide › python › command-line
Python Command Line Application - Vanilla Project
You want to learn a new language? Great · Our aim is to provide guides in various languages and different tooling to help you get started with a new environment
🌐
DEV Community
dev.to › kanakos01 › create-a-cli-application-with-python-1j37
Create a CLI application with Python - DEV Community
February 9, 2025 - from setuptools import setup setup( name="calcli", version="6.9", packages=["calcli"], entry_points={ "console_scripts": ["calcli = calcli.__main__:main"], }, description="A CLI calculator using argparse", author="Your Name", url="Project URL", python_requires=">=3.6", ) ... entry_points - stores the commands that will be accessible through the CLI.
🌐
LibHunt
libhunt.com › l › python › topic › command-line-application-development
Top 17 Python Command-line Application Development Projects | LibHunt
Which are the best open-source Command-line Application Development projects in Python? This list will help you: python-fire, Gooey, typer, click, python-prompt-toolkit, docopt, and asciimatics.