Package: pier-mob-lecovi
Sample Python CLISample repository for creating a CLI in Python.MotivationThis repository is for educational purposes. I want to show how to create an executable
CLI tool written in Python. With this I wanna cover:Creating a Python Package using traditionalsetup.py.Creating a Python Package usingpoetry.Publishing to PyPI.Creating a simple CLI tool that installs on to your system and is executable.Creating plugins for the CLI tool that arepipinstalable.PackageWe are going to create a package calledpier-mob. Why? Just because! I don't wanna use
traditionalexample,foo, orbarbecause I think those names lack semantics
and the use of such names becomes difficult for less experienced devs to understand. If
you are wondering, I letrokechoose the name for the
example tool that we are going to build.Add files and directories:pier_mob
|-- LICENSE               # License file
|-- README.md             # **This** file
|-- pier_mob              # Main directory: all source code inside
|   |-- __init__.py       # needed for converting directory into a package
|   |-- __main__.py       # Defines what is executed when package is called
|   `-- cli.py            # Sample source code module
|-- setup.py              # Python Packaging config file
`-- tests                 # Test cases directory
    |-- __init__.py       # needed for converting directory into a package
    `-- test_version.py   # Test source code sampleWith this minimal structure we can run our program as module and as a simple script.
Usingpipstrategy to be
able of running in both ways. Look deeper onpier_mob/__main__.py.As simple script:$python3pier_mob
PierMobv0.0.1And running as module:$python3-mpier_mob
PierMobv0.0.1Also with this minimal structure we have tests.Tests can be run as:$python3setup.pytestLet's publish our packageYou will needsetuptools,wheel, andtwinefor creating and uploading to PyPI.
Install this packages withpipin new virtualenv or in your user level packages.Create a new virualenv, activate it and install needed packages:$python3-mvenvpier_build
$sourcepier_build/bin/activate(pier_build)$pipinstallsetuptoolswheeltwineNow we need to build source distribution files. Run the following command:(pier_build)$python3setup.pysdistbdist_wheelThis command will output a lot of text and once completed will generate two files in thedistdirectory: a.tar.gzwhich is a Source Distribution, and a.whlwhich is a
Built Distribution.Now we are ready to upload our package. Use your PyPI account and create a new API Token.
Whentwineprompts your username write__token__then use your API Token as password.(pier_build)$python3-mtwineupload--repository-urlhttps://upload.pypi.org/legacy/dist/*
Uploadingdistributionstohttps://upload.pypi.org/legacy/
Enteryourusername:__token__
Enteryourpassword:✨ 🍰 ✨ Congrats! Your package is now published on PyPI:https://pypi.org/project/pier-mob-lecovi/Create a new virtualenv and install your package from PyPI$python3-mvenvvenv
$sourcevenv/bin/activate(venv)$pipinstallpier-mob-lecovi
...
$pythonInside REPL run:>>>importpier_mob>>>pier_mob.version()'0.0.1'✨ 🍰 ✨ It works!!Test PyPIIt's a complete separate instance. If you have a user on PyPI you need to create another
one for Test PyPI (and vivecersa). The same goes for Tokens.Upload URL:https://test.pypi.org/legacy/Pip download URL:https://test.pypi.org/simple/(remember to add--index-url)ExecutableLet's changepier_mob/__main__.pyfor creating a console
script instalable. Wrap theprintfunction insidemain()and modifysetup.pyentry_points={'console_scripts':['pier=pier_mob.__main__:main',],},This will create apierexecutable that will call themain()function inside__main__.py.Create a new virtualenv and test installation:$python3-mvenvexecutable
$sourceexecutable/bin/activate(executable)$python3setup.pyinstall(executable)$pier
PierMobv0.0.2Adding commandsLet's improve our interface usingTyper.
Addinfo()andversion()functions tocli.py.DON'T FORGET TO UPDATE__version__.
You also will need to update__main__and__init__and import the newappinstead
ofversion().
We need to change our unittest for the betterpytestsuite.(install)$pipinstalltyperpytestNow test must be run with:(install)$pytestNow you can update your package on PyPI.(build)$python3setup.pybuild(build)$python3-mtwineupload--repositorypypidist/*ResourcesPackaging PythonHow To Package Your Python Code