当前位置: 首页 > article >正文

什么是 PyPI(Python Package Index,Python 包索引)?

中文版

什么是 PyPI(Python 包索引)?

在 Python 开发过程中,PyPI(Python Package Index,Python 包索引)是一个不可或缺的工具,它是一个用于存储和分发 Python 包的中央存储库。开发者可以通过 PyPI 下载第三方包,也可以将自己开发的包上传到 PyPI 供其他开发者使用。简单来说,PyPI 就是 Python 开发者的“应用商店”。

在这篇博客中,我们将详细介绍 PyPI 的概念、如何使用 PyPI、它的作用以及与 PyPI 相关的竞争产品等内容。

1. PyPI 是什么?

PyPI(Python Package Index) 是一个官方的第三方 Python 软件包的存储库,它是 Python 开发者社区的中心,提供了数以万计的开源 Python 包。Python 包是为了扩展 Python 语言功能的代码集合,涵盖了从数据处理、机器学习到 web 开发等多个领域的工具和库。

  • 官方地址: https://pypi.org

2. PyPI 的作用

PyPI 的核心作用是为 Python 社区提供一个便捷的地方来存储、下载、安装和分发 Python 包。它作为 Python 软件包的中央集成点,具有以下几个主要功能:

2.1 存储和分发 Python 包

开发者可以将自己的 Python 包上传到 PyPI,这样其他用户就能方便地通过 pip 命令安装这些包。上传到 PyPI 的包可以被公开或设置为私有,取决于开发者的需求。

2.2 安装第三方库

PyPI 是使用 pip 命令安装第三方库的默认源。通过 PyPI,开发者可以轻松地安装成千上万的库,而无需手动下载和配置。例如,执行以下命令就可以安装所需的包:

pip install numpy

这个命令会自动从 PyPI 下载和安装 numpy 包及其依赖项。

可以去官网看这个包的具体信息: https://pypi.org/project/numpy/#files

在这里插入图片描述
在这里插入图片描述

2.3 提供包的版本控制

PyPI 允许开发者为每个包发布多个版本,并且开发者和用户可以轻松地获取到不同版本的包。这样,开发者可以管理包的版本,并通过设置版本号来确保与其他包的兼容性。例如,如果某个包在新版本中有重大变化,开发者可以选择在安装时使用旧版本。

3. 如何使用 PyPI?

3.1 安装包

PyPI 最常用的功能就是通过 pip 安装包。默认情况下,pip 使用 PyPI 作为安装源。通过执行以下命令,我们可以安装任何一个公开发布的包:

pip install package_name

其中 package_name 是包的名称。pip 会从 PyPI 下载对应的包并进行安装。例如,安装 requests 包:

pip install requests
3.2 上传包到 PyPI

开发者在完成包的开发后,可以将包上传到 PyPI 供其他开发者使用。上传步骤如下:

  1. 打包:首先需要将 Python 包打包成 .tar.gz.whl 文件。

  2. 创建 setup.py 文件:该文件包含包的元数据,如包的名称、版本、描述等信息。

  3. 使用 twine 工具上传:

twine upload dist/*

通过这一步骤,开发者就能将自己的包发布到 PyPI。

3.3 使用自定义源

如果你不想使用 PyPI 默认源,可以使用其他源。例如,公司内部私有源或者其他公共源。可以通过以下命令来指定使用自定义源:

pip install package_name -i https://custom.pypi.org/simple

4. PyPI 的竞争产品

尽管 PyPI 是 Python 官方推荐的包管理和分发平台,但市场上也有一些其他的包管理工具和平台,可以作为 PyPI 的竞争产品。以下是一些常见的替代品:

4.1 Anaconda(Conda)

Conda 是一个开源包管理器,尤其适用于科学计算和数据分析领域。与 PyPI 不同,Conda 不仅支持 Python 包,还可以管理其他语言的包。它特别适用于需要管理复杂依赖关系和多个环境的情况。Conda 包的存储库是 Anaconda Repository,而且通过 conda 命令安装包时,它会使用 Conda 的索引,而不是 PyPI。

  • 官方网址: https://anaconda.org
4.2 GitHub Releases

对于一些私有或特定用途的项目,开发者可能会将自己的包直接上传到 GitHub Releases,然后通过 pip 直接安装。这种方式适合那些不想通过 PyPI 分发的包,或者临时发布的新版本。

4.3 Google Cloud Artifact Registry

对于需要在云端部署的包,Google Cloud 提供了 Artifact Registry,一个通用的软件包管理系统,支持 Python 包的管理。与 PyPI 类似,Artifact Registry 也可以通过 pip 安装包,但它主要面向企业和大规模分发。

  • 官方网址: https://cloud.google.com/artifact-registry

5. 其他补充资料

除了基础的包管理和分发,PyPI 还为开发者提供了很多其他实用功能:

5.1 包的元数据

每个包都会包含一些元数据(例如,setup.pypyproject.toml 文件等),这些元数据帮助用户和工具了解包的依赖关系、版本要求等信息。

5.2 安全性和验证

PyPI 为上传的包提供了安全性保障,包括包的数字签名和 SHA256 校验和,确保下载的包没有被篡改。开发者也可以为自己的包启用两步验证(2FA)来加强安全性。

5.3 PyPI 镜像

为了提高访问速度,很多国家和地区都有自己的 PyPI 镜像站点。通过选择离自己较近的镜像源,可以加快包的下载速度。

6. 结论

PyPI 是 Python 开发者生态中不可或缺的部分,它为开发者提供了一个集中存储、分享和管理 Python 包的平台。通过 PyPI,开发者可以轻松地安装第三方库,管理包的版本,并确保依赖关系的正常运行。

虽然 PyPI 是最常用的 Python 包索引,但也有一些其他平台可以作为替代品,比如 Anaconda 和 GitHub Releases。不同的工具和平台适应不同的需求,开发者可以根据实际情况选择最适合自己的包管理系统。

英文版

What is PyPI (Python Package Index)?

In Python development, PyPI (Python Package Index) is an essential tool—a centralized repository for storing and distributing Python packages. Developers can download third-party packages from PyPI and also upload their own packages for others to use. In simple terms, PyPI serves as the “app store” for Python developers.

In this blog, we will delve into the concept of PyPI, how to use it, its significance, and the competitive products around it.

1. What is PyPI?

PyPI (Python Package Index) is an official repository for third-party Python software packages. It is a central hub for the Python developer community, offering thousands of open-source Python packages. Python packages are collections of code that extend the functionality of Python, covering a wide range of domains, from data processing and machine learning to web development.

  • Official website: https://pypi.org

2. The Role of PyPI

PyPI’s core role is to provide a convenient place for the Python community to store, download, install, and distribute Python packages. It serves as the central point for Python software distribution and has several key functions:

2.1 Storing and Distributing Python Packages

Developers can upload their Python packages to PyPI, making it easy for others to install them via pip. Packages uploaded to PyPI can be public or private, depending on the developer’s choice.

2.2 Installing Third-Party Libraries

PyPI is the default source for installing third-party libraries using the pip command. Through PyPI, developers can easily install thousands of libraries without the need to manually download and configure them. For example, to install the requests library, you can simply run:

pip install requests

This command automatically downloads and installs the requests library and its dependencies from PyPI.

2.3 Version Control for Packages

PyPI allows developers to release multiple versions of a package, and developers and users can easily access different versions. This helps in managing the package’s version and ensuring compatibility with other packages. If a package undergoes significant changes in a new version, developers can choose to install an older version if needed.

3. How to Use PyPI?

3.1 Installing Packages

The most common use of PyPI is installing packages through pip. By default, pip uses PyPI as the source for packages. You can install any public package from PyPI using the following command:

pip install package_name

For example, to install the numpy package:

pip install numpy
3.2 Uploading a Package to PyPI

Once a developer has finished creating a package, it can be uploaded to PyPI for other developers to use. The steps to upload a package are as follows:

  1. Packaging: First, the Python package must be packaged into a .tar.gz or .whl file.

  2. Creating setup.py: This file contains metadata for the package, such as its name, version, description, and dependencies.

  3. Uploading using twine:

twine upload dist/*

This command uploads the packaged files to PyPI, making the package available for installation.

3.3 Using Custom Sources

If you don’t want to use the default PyPI source, you can use a different source, such as a private repository or another public index. To specify a custom source, use the -i flag:

pip install package_name -i https://custom.pypi.org/simple

4. Competitors to PyPI

Although PyPI is the official package manager and repository for Python, there are a few other package management tools and platforms that compete with PyPI. Below are some notable alternatives:

4.1 Anaconda (Conda)

Conda is an open-source package manager, particularly suited for scientific computing and data analysis. Unlike PyPI, Conda supports managing packages for multiple programming languages. It is particularly useful for managing complex dependencies and multiple environments. Conda uses its own repository, the Anaconda Repository, for Python packages, and conda is used to install packages instead of pip.

  • Official website: https://anaconda.org
4.2 GitHub Releases

For some private or specialized projects, developers may upload their packages directly to GitHub Releases and then install them using pip from a URL. This method is suitable for those who don’t want to distribute their packages via PyPI or want to quickly release a new version.

4.3 Google Cloud Artifact Registry

For cloud-based deployments, Google Cloud provides Artifact Registry, a universal software package management system that also supports Python packages. Similar to PyPI, Artifact Registry can be used to install packages via pip, but it is aimed at enterprises and large-scale distribution.

  • Official website: https://cloud.google.com/artifact-registry

5. Additional Insights

In addition to its core functionalities, PyPI provides several other useful features for developers:

5.1 Package Metadata

Every package on PyPI contains metadata (such as setup.py or pyproject.toml files) that provides essential information about the package, like dependencies, versions, and installation instructions.

5.2 Security and Validation

PyPI provides security features like package signing and SHA256 checksums to ensure that downloaded packages have not been tampered with. Developers can also enable two-factor authentication (2FA) to enhance security when uploading packages.

5.3 PyPI Mirrors

To improve download speed, many countries and regions host their own PyPI mirror sites. By selecting a mirror closer to you, you can speed up package downloads.

6. Conclusion

PyPI is a vital part of the Python ecosystem. It allows developers to easily store, share, and manage Python packages, significantly simplifying the process of installing third-party libraries and managing package dependencies.

While PyPI is the most commonly used Python package index, there are other alternatives like Conda and GitHub Releases, each catering to different needs. Developers can choose the package management system that best fits their requirements, whether it’s for managing dependencies, working with multiple languages, or distributing packages privately.

In the ever-evolving world of Python development, PyPI remains the central hub for package distribution, offering a powerful and accessible platform for developers worldwide.

后记

2024年12月30日17点01分于上海,在GPT4o mini大模型辅助下完成。


http://www.kler.cn/a/459489.html

相关文章:

  • SQL把字符串按逗号分割成记录
  • 【工具整理】WIN换MAC机器使用工具整理
  • 《Vue进阶教程》第三十四课:toRefs的使用
  • 在 SQL 中,区分 聚合列 和 非聚合列(nonaggregated column)
  • 在 macOS 上,你可以使用系统自带的 终端(Terminal) 工具,通过 SSH 协议远程连接服务器
  • 《Xsens动捕与人形机器人训练》讲座将于1月9日下午2:30在线上召开
  • 面试经典150题——数组/字符串(三)
  • 自研国产零依赖前端UI框架实战006 实现表格分页的功能
  • 解决PS 撤销卡顿
  • 【 CSS 】sass 扩展语言的安装
  • IPC$远程植入木马
  • 【YashanDB知识库】yasql / as sysdba无法登录
  • Java设置服务器图片
  • Java-36 深入浅出 Spring - IoC容器体系 BeanFactory过程分析 Bean Lazy-Init
  • Spring Boot集成Netty创建一个TCP服务器,接收16进制数据(自定义解码器和编码器)
  • 纯血鸿蒙ArkUI线性布局详解
  • 【Vue 教程】使用 Vite 快速搭建前端工程化
  • Go singleflight库源码分析
  • 2.阿里云flinkselectdb-jar作业
  • 【React】- 跨域PDF预览、下载(改文件名)、打印
  • Flink如何处理迟到数据?
  • Python毕业设计选题:基于Hadoop 的国产电影数据分析与可视化_django+spider
  • C++ 函数式编程Lambda表达式
  • 磁编码器(Magnetic Encoder)
  • 【每日学点鸿蒙知识】Web嵌套滚动体验、拷贝传递 ArrayBuffer异常问题、ObjectLink 的属性传递、构建读取参数
  • 【高阶数据结构】红黑树封装map、set