Create a CLI Tool with Python: From Zero to Hero
How to create CLI with python
How to create a CLI in Python that can be installed with PIP? - Stack Overflow
I wrote a guide to creating and distributing CLI tools in Python and thought it might be appreciated here!
Hey folks,
I just published a deep-dive guide on building command-line tools in Python
In the post, I cover:
-
Why Python is great for CLI development
-
Setting up the environment (with
click+rich) -
Building a real-world file organizer CLI
-
Adding advanced features (config, logging, progress bars)
-
Packaging & making the tool installable (
setup.py) -
Testing with
pytest -
Best practices for error handling, UX, and deployment
If youβve ever wanted to build your own CLI tools to automate repetitive tasks or just learn how to make polished developer tools, this might help.
I want to make a CLI program in python but I dont know how to do it. I heard a bunch of ways to do a CLI program like use typer or fire but I dont know which is the better option. So I am here to look for an answer of which is the best option to build this type of program in python (It doesn't have to be one of the examples I mentioned)
Use console_scripts to hook to a specific Python method (not a whole executable), setup.py file:
from setuptools import setup
setup(
...
entry_points = {
'console_scripts': ['mybinary=mymodule.command_line:cli'],
},
name='mymodule',
...
)
the command_line.py script would be:
import mymodule
def cli():
print("Hello world!")
and the project directory would look like this:
myproject/
mymodule/
__init__.py
command_line.py
...
setup.py
Setuptools will generate a standalone script βshimβ which imports your module and calls the registered function.
That shim allows you to call mybinary directly and ensures it's correctly invoked by python. It provides platform-specific shims (i.e., on Windows it generates a .exe).
See packaging documentation for more details.
You can do this with setuptools
an example of a nice setup.py (say your package requires pandas and numpy):
import setuptools
setuptools.setup(
name='myscript',
version='1.0',
scripts=['./scripts/myscript'],
author='Me',
description='This runs my script which is great.',
packages=['lib.myscript'],
install_requires=[
'setuptools',
'pandas >= 0.22.0',
'numpy >= 1.16.0'
],
python_requires='>=3.5'
)
Your directory should be setup as follows:
[dkennetz package]$ ls
lib scripts setup.py
inside lib would be:
[dkennetz package]$ ls lib
myscript
inside of myscript would be:
[dkennetz package]$ ls lib/myscript
__main__.py
__init__.py
helper_module1.py
helper_module2.py
main would be used to call your function and do whatever you want to do.
inside scripts would be:
[dkennetz package]$ ls scripts
myscript
and the contents of myscript would be:
#!/usr/bin/env bash
if [[ ! $@ ]]; then
python3 -m myscript -h
else
python3 -m myscript $@
fi
then to run you do: python setup.py install
which will install your program and all of the dependencies you included in install_requires=[] in your setup.py and install myscript as a command-line module:
[dkennetz ~]$ myscript