Publish Python Package with PyPI
PyPI or Python Package Indexing is a great place to publish Python packages, recently I have published a Python package called
funmotd to PyPI. Unlike other tutorials, this post is about my experience that I published real working python app and refered many docs to make it work for specific requirement which we will see in a moment.
First, let’s see what is this
funmotd package, it’s functionality and build the package according to it.
The idea is to display famous Movies and TV Shows quotes on Terminal as motd when you open. I want to make the app simple, have local quotes db, should configurable via CLI and easy to install; which boil down below points.
- The app has local db files (python files)
- The app has configuration text file
- The app should accessible via CLI that user can interact
- Easy to install
Above is my project tree structure which typical file structure for python package.
funmotdDirectory - Contains actual code (
funmotddirectory indicates that
funmotdis package. More info here)
setup.py- Packages information
MANIFEST.in- Used to specify include/exclude files
setuptools is another python package or module designed to facilitate packaging Python projects. Let’s see step-by-step process, how to pack and publish
Install required packages and create account on https://pypi.org/
setup.py which is build script like below
Most of the arguments are self-explainatory, let’s go through some interesting arguments.
Name of the distribution which should be unique on pypi.org
List of packages/sub-packages or simply use
setuptools.find_packages() finds all pacakges automatically
As I said, we want to install package as CLI, with the help
entry_points, we can call our function which contains CLI code(
argparse). The syntax would be
For example, if you have project structure like below and you want to call a function
cli.py which in
sub_pkg directory, the syntax should like below
If the function that want to call is in
__init__.py, then you just have to specify
<pkg_name>:<function_name> like below