Writing a CLI tool in Python and publishing to PyPI

Freddie Wang
1 min readSep 21, 2018

--

PyPI is the official package management system for Python. Creating a Python package and upload to PyPI is not difficult. Not only the package, but some CLI tools (e.g. Jupyter) also can be installed via PyPI. You might be wondering how to do that. Actually, it is also very simple.

First, you create the package then add a wrapper python script to invoke your package. Assuming the package has a main()function, then you can create a wrapper like this

#!/usr/bin/env python 
from your_package import package
if __name__ == "__main__":
package.main()

Then adding thescripts in the setup.py

setup_info = dict(
...
scripts=["bin/wrapper"],
)

Finally, pushing the package to PyPI.

That’s it! You can download your CLI tool from PyPI now

--

--

Freddie Wang

I’m a software engineer from Taiwan and living in Japan now.