To distribute Python packages and projects across communities, there are many tools available. We will focus only on the tools that are recommended as per the guidelines provided by PyPA.
In this blog, we will be covering installing and distributing packaging techniques. A few tools that we will use or are at least worth mentioning in this section as a reference are as follows:
• distutils: This comes with Python with base functionality. It is not easy to extend for complex and custom package distribution.
• setuputils: This is a third-party tool and an extension of distutils and is recommended for building packages.
• wheel: This is for the Python packaging format and it makes installations faster and easier as compared to its predecessors.
• pip: pip is a package manager for Python packages and modules, and it comes as part of Python if you are installing Python version 3.4 or later. It is easy to use pip to install a new module by using a command such as pip install
<module name>.
• The Python Package Index (PyPI): This is a repository of software for the Python programming language. PyPI is used to find and install software developed and shared by the Python community.
• Twine: This is a utility for publishing Python packages to PyPI.
In the next subsections, we will update the masifutil package to include additional components as per the guidelines provided by PyPA. This will be followed by installing the updated masifutil package system-wide using pip. In the end, we will publish the updated masifutil package to Test PyPI and install it from Test PyPI.
Building a package as per the PyPA guidelines
PyPA recommends using a sample project for building reusable packages and it is available at https://github.com/pypa/sampleproject. A snippet of the sample project from the GitHub location is as shown:
Let me introduce key files and folders, which are important to understand before we use them for updating our masifutil package:
• setup.py: This is the most important file, which has to exist at the root of the project or package. It is a script for building and installing the package. This file contains a global setup() function. The setup file also provides a command-line interface for running various commands.
• setup.cfg: This is an ini file that can be used by setup.py to define defaults.
• setup() args: The key arguments that can be passed to the setup function are as follows:
a) Name
b) Version
c) Description
d) URL
e) Author
f) License
• README.rst/README.md: This file (either reStructured or Markdown format) can contain information about the package or project.
• license.txt: The license.txt file should be included with every package with details of the terms and conditions of distribution. The license file is important, especially in countries where it is illegal to distribute packages without the appropriate license.
• MANIFEST.in: This file can be used to specify a list of additional files to include in the package. This list of files doesn't include the source code files (which are automatically included).
• <package>: This is the top-level package containing all the modules and packages inside it. It is not mandatory to use, but it is a recommended approach.
• data: This is a place to add data files if needed.
• tests: This is a placeholder to add unit tests for the modules.
As a next step, we will update our previous masifutil package as per the PyPA guidelines. Here is the new folder and file structure of the updated masifutilv2 package:
We have added data and tests directories, but they are actually empty for now. We will evaluate the unit tests in a later chapter to complete this topic.
The contents of most of the additional files are covered in the sample project and thus will not be discussed here, except the setup.py file.
We updated setup.py with basic arguments as per our package project. The details of the rest of the arguments are available in the sample setup.py file provided with the sample project by PyPA. Here is a snippet of our setup.py file:
from setuptools import setup
setup(
name='masifutilv2',
version='0.1.0',
author='Muhammad Asif',
author_email='ma@example.com',
packages=['masifutil', 'masifutil/advcalc'],
python_requires='>=3.5, <4',
url='http://pypi.python.org/pypi/PackageName/',
license='LICENSE.txt',
description='A sample package for illustration purposes',
long_description=open('README.md').read(),
install_requires=[
],
)
With this setup.py file, we are ready to share our masifutilv2 package locally as well as remotely, which we will discuss in the next blog.
0 comments:
Post a Comment