PyPI Package Publishing: Step-by-Step Guide for Python Project Packaging

CodeStax.Ai
4 min readAug 31, 2023

--

What is PyPI?

PyPI (Python Package Index) is a repository and distribution service for Python software packages. It serves as a central hub where developers can publish their Python packages, making them available for installation by others. PyPI plays a crucial role in the Python software ecosystem by providing a platform for sharing, discovering, and distributing Python packages.

Why do we need to package our project?

Packaging your Python project is essential for sharing, reusability, and maintaining code quality. It enables easy distribution, dependency management, and versioning. Packaged projects can be installed globally or within virtual environments, promoting modularity, isolation, and reproducibility. By providing clear documentation and defining dependencies, packaging enhances collaboration, testing, and continuous integration. It ensures code’s usability across different systems, easing deployment to production environments. Packaging aids code organization, simplifies updates, and streamlines code maintenance, ultimately fostering code reliability and successful collaboration.

Some commands require the latest version of pip. Let’s start by making sure that you have the latest version installed.

In Unix/macOS:

python3 -m pip install --upgrade pip

In Windows:

py -m pip install --upgrade pip

Organize Your Project:
Organize your project’s code into a directory structure that makes sense. Here’s a simple example:

Directory structure to package the project

In this structure:

  • my_package is the root directory of your project.
  • my_project is the package containing your code files.
  • The __init__.py file in a directory signals that the directory should be recognized as a Python package.
  • module1.py and module2.py are the parts where you’ll put your code.

You would modify the content of these files as needed for your project’s functionality and documentation.

Remember that the setup.py file provides metadata about your package, such as its name, version, author, and other details. It’s also used for building and distributing your package. README.md contains project documentation, and LICENSE contains information about the license you're using for your project.

setup.py

from setuptools import setup, find_packages
setup(
name='enter the project name',
version='0.1',
description='add project description',
author='Your Name',
author_email='name@gmail.com',
packages=find_packages(),
)

README.md

# Package
Here , you can give a description of your project.## InstallationYou can install the package using pip:pip install -i https://test.pypi.org/simple/ <name>

LICENSE

In this example, the `README.md` file provides installation instructions, an example of how to use the package, and a note about the project's license. Including a well-written `README.md` helps users understand what your package does and how they can use it effectively.

Navigate to your project directory:

cd Desktop/my_package

Run the commands

Make sure that you have already registered in TestPyPI (it is an isolated environment within the Python Package Index that enables experimentation with distribution tools and procedures without impacting the real index.) and PyPI.

Now, run the following commands one after another in the terminal where you navigated previously:

# Build the distribution package,and it will generate a folder named my_project.egg-info with 4 files listed: dependency_links.txt, PKG-INFO, SOURCES.txt, top_level.txt
python setup.py sdist bdist_wheel
# Install twine if not already installed
pip install twine
# Upload to TestPyPI
twine upload --repository-url https://test.pypi.org/legacy/ dist/*

The terminal will be prompted for a username and password. Give the name that you registered in TestPyPI and PyPI.

Uploading distributions to https://test.pypi.org/legacy/
Enter your username: <username>
Enter your password: <password>
Uploading my_project_sample_add_two-0.1-py3-none-any.whl
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.2/5.2 kB • 00:00 • ?
Uploading my-project-sample-add-two-0.1.tar.gz
100% ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.4/4.4 kB • 00:00 • ?
View at:
https://test.pypi.org/project/<project-name>/0.1/

Install from TestPyPI:

Now you can test installing your package from TestPyPI. Create a new virtual environment, activate it, and install the package.

python -m venv add_one_env
add_one_env\Scripts\activate#this package link you can get from your profilepip install -i https://test.pypi.org/simple/ <project-name>

If everything goes as expected in this TestPyPI, then do the same with PyPI, which is the real index.

Both TestPyPI and PyPI are repositories for hosting Python packages, PyPI is the primary repository for production-ready packages, and TestPyPI is used for testing and staging before packages are uploaded to PyPI. (It’s a best practice to use TestPyPI to ensure your packaging process works correctly before publishing your package on the main PyPI.)

Conclusion:

Awesome! You have published your Python package successfully. Here, you can find the code sample.

In conclusion, packaging and publishing Python projects to PyPI is essential for sharing code globally. PyPI, the Python Package Index, acts as a central repository for Python packages, enabling easy installation and distribution. By organizing code, creating metadata with setup.py, and utilizing twine for uploads, developers ensure code reusability and collaboration. TestPyPI serves as a staging ground to validate distribution before publishing on the main PyPI. This streamlined process enhances code sharing, facilitates testing, and contributes to a robust Python ecosystem.

About the Author

Abitha Thangavel started her journey as a Software Engineer Intern at CodeStax.Ai. She is an enthusiastic learner to explore more about new technologies with a passion for problem solving and enhance her skills.

About CodeStax.Ai

At CodeStax.Ai, we stand at the nexus of innovation and enterprise solutions, offering technology partnerships that empower businesses to drive efficiency, innovation, and growth, harnessing the transformative power of no-code platforms and advanced AI integrations.

But the real magic? It’s our tech tribe behind the scenes. If you’ve got a knack for innovation and a passion for redefining the norm, we’ve got the perfect tech playground for you. CodeStax.Ai offers more than a job — it’s a journey into the very heart of what’s next. Join us, and be part of the revolution that’s redefining the enterprise tech landscape.

--

--

CodeStax.Ai
CodeStax.Ai

Written by CodeStax.Ai

Tech tales from our powerhouse Software Engineering team!

No responses yet