Create python package


You have created some code that solves a specific problem and think others probably have the same problem. To help them you want to create and publish your own python package that can be installed with pip. But you don’t know how?


To create your own python package you need to do the following steps:

  • Register on
  • Install requirements
  • Setup folder structure
  • Compile your package
  • Test on your local machine
  • Upload

Register on

Many packages are hosted on To be able to easily share your package and give others the option to install it like any other package, you need to host it on PyPi.

To be allowed to do so you need to register there. Jump to, register your account and finish the email validation.

Install requirements

Setuptools: The standard package development process library for python

Wheel: The package to create the .whl files that pip installs.

Twine: Have a secure, authenticated, and verified connection with for uploading your package.

Tqdm: Progress meter internally used by Twine

To install them run:

python -m pip install –upgrade pip setuptools wheel

python -m pip install tqdm

python -m pip install –upgrade twine

You can run this in your virtualenv if you want and don´t need to handle system or user level problems.

Setup folder structure

To use the best practice from, create a main folder for your project, e.g. my-package.

Change into the directory, with cd my-package

Inside of it create the following files:


In the same folder create the subfolders:



Finally create a file inside of my-package\my-package. This will contain (some of) your source code.


Go to and select a license. Then copy the provided license text into your LICENSE file. That´s it.

This file can contain the longer explanation and examples for your code in MarkDown. Fill as required.

This file describes the details of your package, requirements, category, etc. It is equivalent to package.json for nodejs.

The following template is a copy with many thanks from :

import setuptools

with open("", "r", encoding="utf-8") as fh:
    long_description =

    name="example-pkg-YOUR-USERNAME-HERE", # Replace with your own username
    author="Example Author",
    author_email="[email protected]",
    description="A small example package",
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",

Fill in your data.

Author and author email need to match your account. Link it to your hosting.


This folder will contain the different ci/cd tests for your code. In the beginning it´s probably emptry.


This file will contain the part of your code that you want to expose, the functions you want the user to be able to call directly from your package.

It´s the entry point to your code, think def main().

Your remaining code will find it´s place inside my-package and subfolders.

final directory structure (user test and home directory in /home: /home/test as base):


Compile your package

To compile your package into a .whl file, execute:

python bdist_wheel

Test on your local Machine

To test your package before you upload, install it with:

python -m pip install dist/my-package*

Look into the dist folder and get the correct naming. It depends on the data you supplied


As preparation for the upload of the package, you need to create a ~/.pypirc (in your home directory, not the local package directory)

Add this content inside of it (adjust username):

repository =
username = my_pypi_username

Now run

python -m twine upload dist/*

It may ask you for your password. In that case please enter it.


You now have created your own python package. And if someone wants to use it, they can run pip install $yourPackageName as they are used to.

Try it!

Let me know when it helped you.



Leave a Reply