Uploading Python Packages Projects to PyPI

  1. Knowledge Base
  2. Programming
  3. Uploading Python Packages Projects to PyPI
  1. Create an account on PyPI.org
  2. Create a repository on GitHub.com
  3. Create a virtual environment if you have not done so already.
  4. In your Python project folder create the following files if they are not created already:

    /packagename/
    __init__.py
    ... all other package files ...
    LICENSE
    README.md

    dist
  5. Active our virtual environment
  6. Install setuptools, wheel, and twine Python packages using pip:
    pip install setuptools
    pip install wheel
    pip install twine
  7. Create and/or update the setup.py file with any changes such as version number, etc.
    import setuptools

    with open("README.md", "r") as fh:
    long_description = fh.read()



  8. Now, from the parent folder which contains the setup.py and the /dist folder run:
    python3 setup.py sdist bdist_wheel
  9. The dist folder will now contain the generated package.
  10. If you have not already done so, create a PyPI API token, log into your PyPI account and navigate to Account Settings:
    https://pypi.org/manage/account/

    Scroll down to API Token and click Create New API token. If it is your first token, name it all and apply it to Entire account (all projects), otherwise you will have the option of creating project specific tokens. Paste the token content into a file in ~/.pypirc

    The ~/.pypirc file should look like this:
    [pypi]
    username = token
    password = pypi-AbEIdHlwaS5vcbcCJDA3MmE0MDU4LTgwNWItNGNmOC1iOTFmLWFmYTI0NjhkZjdkMgACJXsacGVybWlzc2lvbnMiOiAidXNlciIsICJ2ZXJzaW9uIjogMX0AAAYgkFxSwKasOCr-tNINWu-ILOlebpi6kV7_LkqQtLAfBG8

  11. Upload the package to your PyPI account by navigating to the project parent folder and running:
    python3 -m twine upload --repository pypi dist/*

Official PyPI Documentation found here.

Leave a Reply

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