Creating Python Packages
Aman Bhardwaj
Senior Software Engineer at Query | CyberSecurity | I love building Software Stuff
Introduction
Once we have written our code in a Python module, it should be available to use. Now, the next step is to package modules and upload the created package to a common repo from where it can be downloaded. To know more about Python modules, you can read my article here.
What is the pip?
For now,?let’s understand that pip is a tool that allows us to install and manage packages that aren’t part of Python’s standard library. Continue reading to know more about it.
Project folder structure
Let's create the project's folder structure as shown in the below image:
Now, let's add more files like LICENSE, pyproject.toml and README.md to the structure as shown below. These files are relevant to the packages that will be generated. We will take a look at them one by one below:
Before we understand them one by one, let us take a look at the three main components that are essential while creating Python packages:
Front-end tools like pip make use of the backend tools like setuptools to perform the build. But, how does pip know which backend tool to use?
That is where the pyproject.toml file comes in. PIP reads the pyproject file to understand the build-system and project metadata. We will see about project metadata below.
An example of build-system configuration that makes use of setuptools as a backend tool:
Now, let's take a look at the below sample project metadata configuration that will be included in the pyproject.toml:
Configuring the project’s metadata:
领英推荐
Creating a LICENSE
LICENSE is really important for us if we are uploading our Python packages to the Python Package Index, PyPi.
This imposes terms on the users who are installing the package under which they will be using the package.?
A license can be built using https://choosealicense.com/
Generating distribution packages
Run the below commands in the directory where pyproject file is present:
python3 -m pip install --upgrade build
python3 -m build
Running the above commands will create a dist folder for distribution packages and will contain the below files:
Here tar.gz is the source distribution and .whl file is the built distribution. The latest versions of pip will install the built distribution of your package, but it is good to generate both as in some cases they default to the source distribution.
Uploading the packages to TestPyPi
TestPyPi similar to PyPi, but is meant for testing your packages. So, begin by registering here. Also, verify your email address to be able to upload your packages. For more help, please check out this. Once, the registration is complete, please generate the token here.
The next step is to upload the packages. We are going to use twine for doing the same.
Start by running the below commands:
python3 -m pip install --upgrade twine
python3 -m twine upload --repository testpypi dist/*
This will upload both the distribution packages present in dist folder.
Once, the upload is successful, we should be able to see it at https://test.pypi.org/project/<OurPackageName>
That's all for this one and I hope it was useful. Please follow me on Linkedin for more useful articles.