Publishing my 1st Python Package to PyPI

I own a easy-to-remember domain router.hk for years. I develop a IP address lookup which helps Linux & network administrators and end users. Last week, I develops a newer version of API which can return IP address by a simply curl command.

This week, I try to develop a Python package for it and upload to PyPI (Python Package Index). Developing a python package requires better file structure and additional files.

PyPI: https://pypi.org/project/routerhk/
GitHub: https://github.com/routerhk/routerhk

File Structure

routerhk/
├── routerhk/
│ ├── init.py
│ └── routerhk.py
├── tests/
│ └── test_routerhk.py
├── setup.py
├── README.md
├── requirements.txt
└── LICENSE

  1. My package has the above structure.
  2. Develop a class RouterHk in routerhk.py, call it from routerhk/init.py, test the class and functions manually.
  3. Write a test_routerhk.py, I use unittest.
    1. python -m unittest tests/test_routerhk.py
    2. I may change it back to pytest later.
  4. Refer to other python packages to create setup.py which lets PyPI
    1. pip install setuptools wheel
    2. python setup.py sdist bdist_wheel
    3. pip install .
    4. Test the installed local python package from other directory.
  5. Upload to PyPI
    1. pip install twine
    2. twine upload dist/*
    3. Input API Token and upload the package to PyPI.

My PyPI account was created on 2016. I creates a API token on pypi.org and use it to upload the package.

Example code using routerhk:

from routerhk import RouterHK
r = RouterHK()
r.get_ip()

References:

About Sammy Fung

Sammy Fung is a passionate IT technology leader and speaker with deep expertise in open-source software, open data, and Python. Currently, as a developer relations professional, Sammy drives developer engagement and advocacy, fostering vibrant tech communities and empowering developers through impactful initiatives.

Leave a Reply

Your email address will not be published. Required fields are marked *

*