[Python Basics] Python package management: PyPI, setuptools and wheel

Abstract:
This article will introduce the basic concepts of Python package management, focusing on three important tools: PyPI, setuptools and wheel. By digging into how they work and how to use them, we'll show you how to create, distribute, and install Python packages. This article also demonstrates how to apply these tools with practical code examples.

1. Introduction to Python package management

Python package management refers to the process of creating, publishing, and installing Python packages. A Python package is a way of packaging and distributing Python code so that it can be used and shared by other developers. This article will detail three key components of Python package management: PyPI, setuptools, and wheel.

2. Introduction to PyPI

2.1. Principle of PyPI

The Python Package Index (PyPI) is an online repository for publishing and finding Python packages. PyPI allows developers to upload their packages and provides a centralized search engine for other developers to find and install those packages. PyPI uses pip (Python Package Installer) to install and manage packages.

2.2. How to use PyPI

Installation package:

pip install <package_name>

Upgrade package:

pip install --upgrade <package_name>

Uninstall package:

pip uninstall <package_name>

2.3. PyPI example

Here is an example of using pip to install and manage packages from PyPI:

# 安装requests包
pip install requests

# 升级requests包
pip install --upgrade requests

# 卸载requests包
pip uninstall requests

3. Introduction to setuptools

3.1. The principle of setuptools

setuptools is a Python package management tool for creating, building and publishing Python packages. setuptools simplifies the package management process by providing an easy-to-use command-line interface and configuration files such as setup.py. By using setuptools, developers can easily package their code into distributable formats, such as source code distribution (sdist) and wheel distribution (bdist_wheel).

3.2. How to use setuptools

Install setuptools:

pip install setuptools

Create setup.py file:

from setuptools import setup, find_packages

setup(
    name="my_package",
    version="0.1",
    packages=find_packages(),
    install_requires=[
        "requests",
    ],
)

Build the source code distribution:

python setup.py sdist

Build the wheel distribution:

pip install wheel
python setup.py bdist_wheel

Publish the package to PyPI:

pip install twine
twine upload dist/*

3.3. setuptools example

Here is an example of using setuptools to create and publish a Python package:

# 安装setuptools和wheel
pip install setuptools wheel
# 创建setup.py文件
from setuptools import setup, find_packages

setup(
    name="my_package",
    version="0.1",
    packages=find_packages(),
    install_requires=[
        "requests",
    ],
)
# 构建源代码分发和wheel分发
python setup.py sdist bdist_wheel

# 安装twine并发布包到PyPI
pip install twine
twine upload dist/*

4. Wheel introduction

4.1. Wheel principle

wheel is a Python distribution format used to improve installation speed and compatibility. In contrast to source distributions (sdist), wheel distributions are precompiled, which means they don't need to be compiled during installation. This gives the wheel distribution an advantage in terms of installation speed and cross-platform compatibility.

4.2. How to use the wheel

Install wheels:

pip install wheel

Create a wheel distribution:

python setup.py bdist_wheel

Install the package distributed by wheel:

pip install <path_to_wheel_file>

4.3. wheel example

Here is an example of creating and installing a Python package using wheel:

# 安装wheel
pip install wheel

# 创建wheel分发
python setup.py bdist_wheel

# 安装wheel分发的包
pip install dist/my_package-0.1-py3-none-any.whl

5. Summary

This article introduces the basic concepts of Python package management in detail, focusing on the three important tools of PyPI, setuptools and wheel. We've shown you how to create, distribute, and install Python packages by digging into how they work and how to use them. Real code examples give you the means to apply these tools in real projects.

6. References

  1. Python official documentation: Python Packaging User Guide
  2. Setuptools official documentation: setuptools documentation
  3. Wheel official documentation: wheel documentation

Thank you for reading this article. If you think this article is helpful to you, please follow us and give a reward. Your support is our motivation to continue creating!

Guess you like

Origin blog.csdn.net/qq_33578950/article/details/130297451