Publishing Your Python Package on PyPI: A Simple Guide

Fatima Medlij
3 min readJul 17, 2023

--

Creating and publishing a Python package on PyPI allows you to share your code with the community and make it easily installable for others. In this article, we will walk through setting up a PyPI account, organizing your package files, configuring the necessary files, and finally, uploading your package to PyPI.

Step 1: Create a PyPI Account

Go to https://pypi.org/ and create an account on PyPI. Fill in the required details and complete the registration process. Once you have your PyPI account, you’ll be able to publish and manage your Python packages.

Step 2: Set Up the File Structure

Create a directory for your package and organize the files as follows:

file-structure
  • __init__.py: Keep this file empty. It is required to mark the directory as a Python package.
  • packagelogic.py: Add your function logic to this file. Make sure to structure your code in a way that prompts the user to import only the necessary functions or modules.
  • README.md: Add a brief description of your package and provide instructions on how to use it effectively. This file serves as the documentation for your package.
  • setup.py: The setup.py file is crucial for your package as it acts as the build script and provides essential information about the package, including its version, dependencies, and required files.
    Use the following template:
from setuptools import setup

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

setup(
name='my_package_name',
version='1.0.0',
description='A description of your package',
long_description=long_description,
long_description_content_type="text/markdown",
author='Your Name',
author_email='your@email.com',
packages=setuptools.find_packages(),
install_requires=[
'dependency1',
'dependency2',
],
license='MIT'
)

Note: Verify that the name of your package is available on PyPI to avoid a 403 Client Error when uploading the files.

  • LISENSE: Select a license for your package. While you can choose any license, the MIT License is a popular choice for open-source projects. Below is the MIT License template:
MIT License

Copyright (c) [year] [your name]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Step 3: Generate Distribution Archives and Upload the Package

Open your terminal or command prompt and execute the following commands:

3.1. Install/Upgrade Setuptools

python -m pip install --user --upgrade setuptools wheel

3.2. Generate the distribution archives

python setup.py sdist bdist_wheel

This command will create a “dist” directory that will contain the source archive.

3.3. Install/Upgrade twine

python -m pip install --user --upgrade twine

3.4. Upload the package

twine upload dist/*

You will be prompted to enter your PyPI account’s username and password. Enter the credentials and press enter. The package will be uploaded to PyPI, making it available for installation by other Python users.

Congratulations! You’ve successfully published your Python package on PyPI.

--

--

No responses yet