Publish Package to PyPI

In this blog we will walk through how to publish packages to PyPI.org.

Prerequisites

The very first requirement is to have an account on PyPI.

We need to install 3 tools globally

  1. wheel
  2. setuptools
  3. twine
pip3 install setuptools wheel twine

Getting Started

We will be publishing the `covin-cli` project to PyPI.

  • As a best practice create a high level directory with the same name as our package name in our case covin-cli.
  • Now create a __init__.py file in covin-cli directory so that Python will see our code as a package.
❯ cd covin-cli
❯ tree
.
└── covin-cli
└── __init__.py
1 directory, 1 file
  • Now you can add your code in a Python file.
  • We need to add 3 files to publish the package to PyPI to the root directory in-order to publish a package to PyPI. setup.py, LICENSE, README

Setup.py

README.org or README.md

A well written README describing about your code.

LICENSE

You can go to https://choosealicense.com/ and choose license which you want to have.

Generate Packages

Now we need to generate 2 packages sdist(Source Distribution) and bdist_wheel (Build Distribution)

python3 setup sdist bdist_wheel

Above command will generate 2 directories build and dist in the dist directory we will have wheel file which is build distribution and a source distribution.

❯ tree
├── dist
│ ├── covin_cli-1.0-py3-none-any.whl
│ └── covin-cli-1.0.tar.gz
0 directories, 2 files

Now the final step is to upload to PyPI. We will use twine to upload our packages

❯ twine upload dist/* --verbose
Uploading distributions to https://upload.pypi.org/legacy/
dist/covin_cli-1.0-py3-none-any.whl (16.5 KB)
dist/covin-cli-1.0.tar.gz (4.5 KB)
Enter your username: abhishekamralkar
Enter your password:
Uploading covin_cli-1.0-py3-none-any.whl
100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 25.0k/25.0k [00:08<00:00, 3.02kB/s]
Uploading covin-cli-1.0.tar.gz

Once the package uploaded it will be available at


https://pypi.org/project/covin-cli/1.0/

To install

❯ pip3 install covin-cli==1.0
Defaulting to user installation because normal site-packages is not writeable
Collecting covin-cli==1.0
Using cached covin_cli-1.0-py3-none-any.whl (16 kB)
Installing collected packages: covin-cli
Successfully installed covin-cli-1.0

To Uninstall

❯ pip3 uninstall covin-cli==1.0
Found existing installation: covin-cli 1.0
Uninstalling covin-cli-1.0:
Would remove:
/home/aaa/.local/lib/python3.7/site-packages/covin-cli/*
/home/aaa/.local/lib/python3.7/site-packages/covin_cli-1.0.dist-info/*
Proceed (y/n)?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Amralkar

Abhishek Amralkar

DevSecOps Practitioner | DevOps Enthusiast | Clojure | Cloud Automation | Cloud Security Enthusiast