Creating Python Packages
Creating Python Packages

Creating Python Packages

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:

No alt text provided for this image
Initial project structure

  1. Only the directories that contain the __init__.py files will be treated as packages, so we need to make sure that we have kept the file appropriately.
  2. mymodule is the name of the module that will be contained within the package.

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:

No alt text provided for this image
A project structure ready for packaging

Before we understand them one by one, let us take a look at the three main components that are essential while creating Python packages:

  1. Front-end tools: pip and build
  2. Backend tools: hatchling, setuptools etc..
  3. pyproject.toml

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:

No alt text provided for this image
A Sample build system configuration in pyproject.toml

  1. requires tell the packages required to build your package. This can contain more packages, not necessarily the backend tool. Don’t confuse it with the project’s dependencies.
  2. build-backend names the build-backend tool that will be used by pip to perform the build

Now, let's take a look at the below sample project metadata configuration that will be included in the pyproject.toml:

No alt text provided for this image
A sample project metadata configuration in pyproject.toml

Configuring the project’s metadata:

  1. name: It is the name by which our package will be known or identified.
  2. version: Mention the version of the package. Say we start with 0.0.1?
  3. authors: Lists the authors and maintainers of the package
  4. description: A short summary of our package
  5. readme: This will contain the path of the file containing detailed information about our package. This can contain the details like how to install the package, how to use it etc.
  6. requires-python: The versions of python supported by our package. This allows the pip to install the right version of the package that matches the version of python installed on our system
  7. classifiers: Tells some more information about the package like, what license the package has, is the package OS dependent or not, etc.
  8. urls: Here we can provide more links related to the package that can be shown on the package hosting index, like PyPi.?

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:

No alt text provided for this image
Source and built distribution packages

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.

要查看或添加评论,请登录

Aman Bhardwaj的更多文章

  • Python Modules - 1: The Basics

    Python Modules - 1: The Basics

    Welcome to my yet another LinkedIn article related to Python. We will try to understand what are Python modules.

  • A refresher on Compilers

    A refresher on Compilers

    Introduction For sure, compilers are one of the most complicated programs ever written. We have been writing code in…

  • Python Decorators - Part lll

    Python Decorators - Part lll

    This article is in continuation to my previous two articles on decorators. I would encourage you to read both of those…

    1 条评论
  • Python Decorators - Part ll

    Python Decorators - Part ll

    ..

    2 条评论
  • Python Decorators - Part I

    Python Decorators - Part I

    Introduction Decorators are one of the widely used concepts in Python. I have not encountered a Python codebase, yet…

    1 条评论
  • Variable Scoping and Closures in Python

    Variable Scoping and Closures in Python

    Introduction Variable scoping works almost the same way in most programming languages with small differences. Though it…

  • Python's property() class

    Python's property() class

    As we know that in languages like Java, C++, classes are supposed to contain private data attributes as a best coding…

社区洞察

其他会员也浏览了