Better Programming

Advice for programmers.

Follow publication

Publishing Your Python Packages to PyPi

Share your packages with the world

Eric Chi
Better Programming
Published in
7 min readSep 16, 2020

--

Photo by the author.

Note: The complete source code can be found on GitHub.

If you work with Python, then you are probably aware of packages that you can download and import into your projects using pip. It’s a robust tool that allows developers from all around the world to upload their code so that others can use it.

In today’s article, I will be showing you how to publish your Python package to the service PyPi, which is the Python Package Index. This is where pip goes to download those sweet packages for you to use in your projects and command-line tools that you can use on your computer.

1. Creating the Project

The first step that we are going to need to take is to create a very simple package for us to publish onto PyPi. This step can be as complex as you’d like depending on the application you wish to publish, but for this example, we will be creating a very simple package.

Let's go ahead and create a new directory structure for our project:

sample-pypi-package
└── hello_world
├── __init__.py
└── main.py

sample-pypi-package is the root level of the project and we are creating a module of hello_world inside the project. The contents of __init__.py are empty and main.py has the contents shown below:

That’s pretty much it for the first step. Nothing out of the ordinary if you are familiar with Python. The main.py file isn’t even necessary for this tutorial, but I added it just so that we have a file within the module.

2. The setup.py File

We will be creating a new file called setup.py that acts as the build script for our package. We will, however, need to have setuptools installed through pip. You can run the command below. Be aware of which version of Python you are using and use the appropriate pip version.

pip install setuptools
OR
pip3…

--

--

Eric Chi
Eric Chi

Written by Eric Chi

Passion for learning, teaching, and helping others understand the world of software. Currently working @ Apple

Responses (1)

Write a response