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()
r.ip_address

References:

About Sammy Fung

With over 20 years of experience in international information technology, promoting the development of open source and open data. and participated in numerous media interviews on technology issues.

Leave a Reply

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

*