How To Create My Own Python Package

How To Create My Own Python Package

Ever wanted to have your own python package on pypi? Ever wondered that your package will be used my a million or even a billion developers for various purposes?

Then this article is for you to easily get started. Let’s jump inside!

Step 1 – Getting Started

First of all create an account in PyPi from here: https://pypi.org/account/register/

Before we get started we need to make sure that our installed pip is latest. To do so, we need to run the pip upgrade command in our terminal:

#For Unix/MacOS:
python3 -m pip install --upgrade pip

#For Windows:
py -m pip install --upgrade pip

#For Python Virtual Environment:
pip install --upgrade pip

Now we need to install the latest version of PYPA’s build to generate distribution packages. Run this command in the terminal:

For Unix/MacOS:
python3 -m pip install --upgrade build

#For Windows:
py -m pip install --upgrade build

#For Python Virtual Environment:
pip install --upgrade build

Now, to upload the distribution packages to pypi we need to install Twine. Run the command in the terminal:

#For Unix/MacOS:
python3 -m pip install --upgrade twine

#For Windows:
py -m pip install --upgrade twine

#For Python Virtual Environment:
pip install --upgrade twine

Now we have meet all the requirements that we need to get started…

Step 2 – Creating The Package Structure

The structure is simple containing one folder, your project license, readme file and other setup files

Here’s how the structure looks like:

Root Folder/
├── LICENSE
├── pyproject.toml
├── README.md
├── Source Folder/
│   ├── __init__.py
│   └── example.py
└── setup.py

Root Folder” is the main folder where every other files will be included and “source folder” is the folder where your package files should be included.

readme.md” is the file where all details and how to use your package should contain. “__init__.py” is required to import the directory as a package, I can either be empty or you can declare the what functions should be included from what files and some other basic things.

Step 3 – Creating the “pyproject.toml”

“pyproject.toml” makes the build tools understand what is required to build your project. We are going to use setuptools . So, open the “pyproject.toml” (check the structure and create one if you haven’t yet) and copy the following content to it:

[build-system]
requires = ["setuptools>=42"]
build-backend = "setuptools.build_meta"

build-system.requires defines the packages that are required to build your package and listing something here will make it available during the build only.

build-system.build-backend is the name of Python object that will be used to perform the build.

Step 4 – Configuring metadata (setup.py)

setup.py tells the setuptools about your package such as name, author, version, website, description, etc.

Open your setup.py (check the structure and create one if you haven’t yet) and copy the following content to it. Make sureto change the name and all other contents inside it:

import setuptools
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()

setuptools.setup(
name="example-package-YOUR-USERNAME-HERE",
version="0.0.1",
author="Example Author",
author_email="[email protected]",
description="A small example package",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
project_urls={
"Bug Tracker": "https://github.com/pypa/sampleproject/issues",
},
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
package_dir={"": "src"},
packages=setuptools.find_packages(where="src"),
python_requires=">=3.6",)

Step 5 – Creating “readme.md”

open “readme.md” (check the structure and create one if you haven’t yet) and check the following basic structure. You can also use Github Flavoured Markdown to beautify it:

# Example Package All

This is a simple example package. You can use
[Github-flavored Markdown](https://guides.github.com/features/mastering-markdown/) to write your content.

We need it to provide the long description of our package as we defined this file that contains our long description in the previous step.

Step 6 – Creating A License

It is required to create a license and include it in your package. This tells users who install your package the terms under which they can use your package. You can take the help of https://choosealicense.com/ to choose a license for your project. Once you have chosen a license, open LICENSE and paste the license text.

Step 7 – Generating Distribution Archives

We are almost done. Now we need to generate the distribution packages. These are archives that are uploaded to the Python Package Index and can be installed by pip.

Now run this command from the root directory where the “pyproject.toml” of our package is:

#For Unix/MacOS:
python3 -m build

#For Windows:
py -m build

This will output a lot of texts in the terminal and finally generate a new directory named “dist” containing two files in it:

dist/
YOUR-PACKAGE-NAME-0.0.1-py3-none-any.whl
YOUR-PACKAGE-NAME-0.0.1.tar.gz

Step 8 – Uploading The Distribution Archives

Finally, we are here! Now it’s time to upload our package to PYPI. We need to run Twine to upload all of the archives under dist. Run the following command in the root directory:

#For Unix/MacOS:
python3 -m twine upload --repository pypi dist/*

#For Windows:
py -m twine upload --repository pypi dist/*

You will be asked to input a username and password. Type your pypi username and password that you use to login. While typing “password” it may not show that what you are typing. don’t worry, just type the password and press enter. After that you may see an output like this:

Uploading distributions to https://test.pypi.org/legacy/
Enter your username: [your username]
Enter your password:
Uploading YOUR-PACKAGE-NAME-0.0.1-py3-none-any.whl
100%|█████████████████████| 5.65k/5.65k [00:01<00:00, 3.88kB/s]
Uploading YOUR-PACKAGE-NAME-0.0.1.tar.gz
100%|█████████████████████| 5.25k/5.25k [00:01<00:00, 4.05kB/s]

Congratulations! You have successfully created a python package and is now live on pypi. Now anyone can use it using pip command.

Thanks for allowing us to help you!

If you are confused or want to know something, then let us know in the comment box, we will reach you as soon as possible. Don’t Forget To Subscribe our Newsletter, YouTube Channel, and Like Our Facebook Page To Keep Updated With Awesome Things. Follow us on Twitter to stay updated with the latest news & changes.

Did you like it?