How to Publish an Open-Source Python Package to PyPI?

0
477
Open-Source Python

Introduction

Python is well-known for including batteries. The standard library includes sophisticated features. Modules for interacting with sockets, parsing CSV, JSON, and XML files, and working with files and file directories are available.

Regardless of how fantastic the bundles are. There are many wonderful projects accessible outside of the standard libraries that are provided with Python. These are usually held at the Python Packaging Index (PyPI), formerly known as the Cheese Shop. Everything from Hello World to complex deep learning libraries may be found on PyPI. You can always acquire the Python Programming training from a recognized institute like KnowledgeHut.

This article will teach you how to post your package to PyPI. While publishing your project is easier than it used to be, there are still a few steps to do.

Here’s a step-by-step instruction for submitting your Python package to PyPI.

  1. Developing the Project

The first thing we’ll need to do is build a very simple package that we can publish to PyPI. This stage may be as complicated as you like depending on the application you want to publish, but for this example, we’ll create a very simple package.

Let’s start by making a new directory structure for our project:

The root level of the project is sample-PyPI-package, and we are constructing a hello_world module within it. The contents of __init__.py are empty, and the contents of main.py are as follows:

That’s all there is to the first step. If you’re familiar with Python, this is nothing out of the norm. The main.py file isn’t required for this lesson, but I included it to have a file within the module.

  1. The setup.py script

We’ll make a new file named setup.py that will serve as the package’s build script. We will, however, require setup tools to be installed through pip. You can use the following command. Keep track of the Python version you’re using and use the appropriate pip version.

pip install setuptools

OR

pip3 install setuptools

Now that setuptools have been installed, we can begin configuring the setup.p

As previously said, this file is in charge of creating our distribution file, which will be posted to PyPi. I left some remarks for each line.

I’d like to draw attention to two lines:

requirements =

[“requests<=2.21.0”]install_requires=requirements,

  1. LICENSE and README.md Files

This section isn’t as interesting, but it’s crucial. Because you are publishing to the public, you must have acceptable documentation as well as a suitable open source license. We also require the README.md because it is referenced in setup.py.

So, on the root level of the project, let’s make a basic README.md:

# Sample PyPi Package

This is a simple exercise to publish a package onto PyPi.

README.md has been completed.

Now, let’s make a LICENSE file on the root level using the MIT license.

Now that we’ve written our LICENSE and README.md files, the directory structure should look something like this:

  1. Producing Distribution Files

So, now that we’ve completed our package, we’re ready to create our distribution files. A distribution file is a file that the user will download and unpack on their workstation in order to run their code. When you execute pip install on a package, you are downloading the tarball/zip to unpack locally on your system.

From here, we’ll need another tool to generate the distribution file:

pip install wheel OR pip3 install wheel

Once you’ve downloaded the wheel, we can use it to generate the files.

Run the following commands in the same directory as setup.py.

python setup.py sdist bdist_wheel OR python3 setup.py sdist bdist_wheel

When you run this, your terminal should show you some logs of what it’s doing. When the command is finished, you should have a few new folders created, which I have highlighted below.

Build and hello_world_ericjaychi.egg-info can both be skipped. We’d want to concentrate on the dist directory:

  1. Uploading the Distribution Files

It’s now time to post these files to PyPi. Before we can accomplish that, we must first sign up for a PyPI account. One point I’d like to emphasize: PyPi provides a testing environment for publishing packages, allowing you to experiment with the process before going live. Both registration forms will be sent by me. I would strongly advise you to experiment with the test site before uploading the official one to the live servers.

  • Production environment
  • Testing environment

I’ll show you some examples of how to publish in both environments.

After you’ve signed up for the various environments, it’s time to install the last program that will allow us to deliver the distribution package to you.

PyPi:

pip install twine OR pip3 install twine

Once you’ve installed twine, you’ll want to use this new tool to post the package to PyPI. Run the following command at the project’s root level:

twine upload –repository testpypi dist/* (TEST)

twine upload –repository pypi dist/* (PROD)

If these commands do not work, add python or python3 before them.

After you input the appropriate command, you will be required to provide your credentials for the environment you selected. When you’re done, you’ll see a few progress bars for the files that are being uploaded. It will even provide you with a link at the end to demonstrate that it has been published for that specific version.

Conclusion

Now that we’ve installed the package, let’s double-check that it was installed successfully. If you somehow got interested in the Python Language, you can always learn python programming online.

You can follow the above steps again for any issues in the package installation. The procedure given is the simplest and most comprehensive way to install the Open source python package to the PyPI. Hope! It has made the process clear and easy for you.