Publish a Python Package like a Pro
By: Sai Durga Kamesh Kota
Tags: Programming, Python3, Python, Package

If you are in Pythoneer/Pythonista or at least familiar with python, you will encounter the word PIP as this is a vital tool that we will use to install a package to import any package in the program. Have you ever thought of creating your package or wanted to know how this process works then you came to the perfect place? This article demonstrates your step to step process so that right away you can publish your package after reading this article.

Steps for publishing a python package in PIP:-

Firstly you need to create a python script you wanted to publish as a package. In my case, for example, I am creating a simple script that will return a welcome message. This is the following script which I want to publish.

2. This file should be saved as <packagename>.py. In my case, my package name is welcomesample so the above script should be saved as welcomesample.py.

3. Now create a new folder and keep it as the name the same as the package name.

4. Now create a subfolder in the project folder with the name “src” and move all the required programs and files for your above script.

5. Now create a setup.py file with the following script below.

In the above script, the setup() file has the following attributes.

name is the distribution name of your package. This can be any name as long as only contain letters, numbers, _ , and - . It also must not already be taken on pypi.org. Be sure to update this with your username, as this ensures you won’t try to upload a package with the same name as one which already exists when you upload the package. In my case, the name attribute value is

is the distribution name of your package. This can be any name as long as only contain letters, numbers, , and . It also must not already be taken on pypi.org. as this ensures you won’t try to upload a package with the same name as one which already exists when you upload the package. In my case, the name attribute value is version is the version number of the package. If it is the first version you can specify 0.0.1. It should update for each update in the package.

is the version number of the package. If it is the first version you can specify 0.0.1. It should update for each update in the package. author refers to the name of the author who published the package.

refers to the name of the author who published the package. author_email refers to the mail of the author. This field is optional but important.

refers to the mail of the author. This field is optional but important. description refers to a short description of the package which is usually 10 to 25 words long.

refers to a short description of the package which is usually 10 to 25 words long. long_description refers to the documentation of the package which will be usually given in form of a markdown file which will be discussed in-depth in the next step.

refers to the documentation of the package which will be usually given in form of a markdown file which will be discussed in-depth in the next step. url refers to the source code of the package. It can be optional but important. Usually, we can put the Github repository URL of the package.

refers to the source code of the package. It can be optional but important. Usually, we can put the Github repository URL of the package. packages refer to a list of all Python import packages that should be included in the Distribution Package. Instead of listing each package manually, we can use find_packages() it to automatically discover all packages and sub-packages.

refer to a list of all Python import packages that should be included in the Distribution Package. Instead of listing each package manually, we can use it to automatically discover all packages and sub-packages. classifiers are used to classify our project in the PyPI filter field. If you want to know more about classifiers you can go to this link.

are used to classify our project in the PyPI filter field. If you want to know more about classifiers you can go to this link. python_requires is used to specify which python version your package works. For this, you need to check manually and fill this field.

is used to specify which python version your package works. For this, you need to check manually and fill this field. py_module refers to the package name. It is mandatory.

refers to the package name. It is mandatory. package_dir refers to the src file path. It is mandatory.

6. After you create the setup.py file then you need to create a README.md markdown file to write the documentation of your package. It should contain details regarding the short description of the package, how to use this package etc. For example, your README.md can look like this:-

# WelcomeSample This is a welcome package which prints a welcome message

The output of the README.md file looks similarly as shown below.

7. Create a Licence file named LICENCE. It’s important for every package uploaded to the Python Package Index to include a license. The best way for finding license templates is by picking a LICENCE Template from GitHub or by choosing from https://choosealicense.com/. For example, if you choose the MIT License format then your license will look like below:-

8. After the above steps are done then the directory structure looks similar to the below shown one. Please check whether your package directory looks similar.

tutorial

├── LICENSE

├── README.md

├── WelcomeSample

│ └── src

└── welcomesample.py

├── setup.py



9. Now you need to generate distribution archives for the package you need to create. These archives help us in installing our package in any OS. For this, you need to run the following command in CMD to install wheel and setuptools.

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

10. After successfully installed the above packages now you need to run the following command to generate the archives file(.gz) and wheel file(.whl) for the package you want to create.

python setup.py sdist bdist_wheel

The output of the above command will generate the 2 directories in your working package directory namely dist and build. If you check the dist folder you can be able to see the 2 files i.e wheel(.whl) and source archive (.gz) files. These are very much important to publish your package. The directory structure looks like this

tutorial

├── build

├── dist

├── welcomesample-0.0.1.tar.gz

├── welcomesample-0.0.1-py3-none-any.whl

├── LICENSE

├── README.md

├── WelcomeSample

│ └── src

└── welcomesample.py

├── setup.py

The reason why we require wheel and source archive files is that the newer pip version supports wheel built distributions but will fall back to source archives if they are needed. If we include both files then it makes our package to install on any platform.

11. Now we can install the package locally in the system by using the following command.

pip install -e .

12. We can test the package locally in your IDE by writing the following code and run it.

import welcomesample

print(welcomesample.welcome())

The output of the above code is shown below.

Hurray! It works fine 😎.

13. Now it's time to ship the code to PyPI. Before that, you need to ship your code firstly to Test PyPI. Test PyPI is a platform similar to PyPI used for experimenting and testing purposes. You need to register on the following websites to proceed further.

14. After creating the account on 2 platforms then we need to install a package twine which helps in shipping the package. For this, you need to run the following command in the CMD.

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

Once the twine package is installed then you need to run the following command below to ship the code to Test PyPI.

python -m twine upload --repository testpypi dist/*

When we run the above command then it asks you for a username and password. Enter the credentials by which we registered an account in Test PyPI. Once we enter these credentials your package is successfully installed in Test PyPI. The output of the above command looks like this shown below.