Skip to content

Instantly share code, notes, and snippets.

@dreamfarer
Last active December 1, 2023 13:27
Show Gist options
  • Select an option

  • Save dreamfarer/c5a9009e76eab0007fc82663247062f8 to your computer and use it in GitHub Desktop.

Select an option

Save dreamfarer/c5a9009e76eab0007fc82663247062f8 to your computer and use it in GitHub Desktop.
How to Publish Your Python Package to PyPI

How to Publish Your Python Package to PyPI

Uploading your Python package to PyPI allows other users to download and use it with pip install.

1. Adjust Package Structure

Ensure your package has a structure similar to this. This is a basic structure but can vary based on specific needs.

your_package_name/
    src/
        your_package_name/
            __init__.py
            module1.py
            module2.py
            sub_package/
                __init__.py
                module3.py
            ...
    tests/
    README.md
    LICENSE
    setup.py
    .gitignore
  • your_package_name/: The top-level directory with your package's name.
  • src/: Contains your package code. Wrapping it in src/ helps avoid common issues with package imports.
  • __init__.py: An empty file indicating that this directory should be treated as a Python package.
  • module1.py, module2.py, ...: Your Python modules.
  • tests/: Directory for your package's test file
  • README.md: A Markdown file with a description, documentation, examples, etc.
  • LICENSE: The license file for your package.
  • setup.py: A setup script with metadata for your package.
  • .gitignore: Specifies untracked files to ignore.

2. Populate setup.py

from setuptools import setup, find_packages

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

setup(
    name="...",
    version="0.0.1", # major.minor.micro
    description="...",
    long_description=long_description,
    long_description_content_type="text/markdown",
    author="...",
    author_email="[email protected]",
    url="...",
    install_requires=["dependency1", "dependency2", ...], 
    package_dir={"": "src"},
    packages=find_packages(where="src"),
    classifiers=[ # Choose appropriate classifiers from https://pypi.org/classifiers/
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License", 
        "Operating System :: OS Independent",
    ],
    python_requires=">=3.10",
)

3. Uploading to PyPI

3.1 Create PyPI Account

Create a PyPI account at PyPI Registration. Set up 2FA for security and generate an API key used to upload your package via twine.

3.2. Store API Key

Create a .pypirc file in your $HOME (Unix/Linux) or %USERPROFILE% (Windows) directory with the following content:

[pypi]
  username = __token__
  password = pypi-AgEIcHlwaS5scmcCJD...

Keep this API key confidential.

3.3 Build Your Package

Install the required tools:

pip install --upgrade setuptools wheel
pip install build

In your package's root directory (where setup.py is located), build your package with:

python -m build

This will create distribution files in the dist/ directory.

3.4 Upload Your Package to PyPI

Install the required tool:

pip install twine

Before uploading, check the dist/ directory to confirm everything is built correctly. Run the following command from within your package's root directory to upload your package to PyPI:

twine upload dist/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment