Uploading a Python Package to PyPI Draft

Posted by Geoff, Published: 2 years, 7 months ago (Updated: 2 years, 6 months ago)

Over your Python experience you have probably used the pip install command many times. As you no doubt have found out, it is a quick and easy way to share and obtain your favorite Python packages. This tutorial is going to show you how to upload your Python packages to PyPI so that your code is available through the pip install command. 

First step is you should make sure that your Python project follows the following file structure:

/python_package_example
  /python_package
    __init__.py
setup.py
LICENSE
README.md

I generally give the python_package_example and python_package variables the same name. This python_package value is the one that people will be using when they evoke the python -m pip install python_package command. You also should define a name variable within the /python_package_example/python_package/__init__.py file, for example:

name = "python_package"

Setup.py Configuration

The setup.py file within the python_package_example directory is crucial for uploading this package. I suggest using the following as a starting point:

import setuptools

# this will go through that README.md file that is within the python_package_example directory and load its contents
with open("README.md", "r") as fh:
    long_description = fh.read()

pkg_name = "python_package"  # the name of your python package

setuptools.setup(
    name= pkg_name,
    # whatever the version is of your code, when you update your package you will need to update this version numbner
    # for the update to work through pypi
    version="1.0.0",
    author="Your Name",  # The author's name (likely your name)
    author_email="YourEmail@example.com",  # the author's e-mail (likely your e-mail)
    description="This is just an example python project.",  # a brief description of the code
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/YourGitHubUsername/python_package.git",  # the link to your GitHub
    packages=setuptools.find_packages(),  
    install_requires=
    [
        'Example',  # I manually put in required packages most of the time. Feel free to put them here.
    ],
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
    ],
)

Now you have to make sure you have the latest version of setuptools and wheel by performing the following command in the command prompt / terminal:

python -m pip install --upgrade setuptools wheel

Next you will generate .whl (wheel) and .targ.gz files that pip uses to install your packages, you can generate these files by using the following command within the directory that contains your setup.py file (python_package_example):

python setup.py sdist bdist_wheel

You should now see a dist directory, this directory will contain the .whl and .targ.gz files.

To upload this dist directory to PyPI, you must first install the twine package using the following command:

python -m pip install twine

Finally, you can upload your Python package to PyPI by executing the following command:

python -m twine upload dist/*

Updating PyPI Package

  1. This part is really simple, you will have to update your version value in the setup.py file to reflect the new version (if you don't change this value it will not let you upload).
  2. Then you just re-create the dist directory with the python setup.py sdist bdist_wheel command.
  3. Then you re-upload using the same twine command: python -m twine upload dist/*

Comments

Post Comment