Python 简明教程
Python - PIP
Pip in Python
在 Python 中, pip 是用于安装和管理用 Python 编写的软件包的标准包管理系统。它允许您轻松地安装库和框架以扩展 Python 应用程序的功能。pip 包含在 Python 中,从 Python 版本 3.4 及更高版本开始。
In Python, pip is the standard package management system used to install and manage software packages written in Python. It allows you to easily install libraries and frameworks to extend the functionality of Python applications. pip comes bundled with Python, starting from Python version 3.4 and above.
Installing pip
如果您使用 Python 3.4 或更高版本,则已包含 pip。但是,如果您尚未安装 pip,则可以使用以下步骤安装它 −
If you are using Python 3.4 or above, pip is already included. However, if you don’t have pip installed, you can install it using the following steps −
-
Download get-pip.py script −
-
Run the Script
Installing Packages with pip
您可以使用 pip 从 Python Package Index (PyPI)安装任何包,PyPI 是 Python 的官方第三方软件存储库。
You can use pip to install any package from the Python Package Index (PyPI), which is the official third-party software repository for Python.
Syntax
以下是使用 pip 在 Python 中安装包的基本语法 −
Following is the basic syntax to install packages with pip in Python −
pip install package_name
Example
要安装 requests 库,可以使用以下命令 −
To install the requests library, you can use the following command −
pip install requests
Upgrading Packages
要将包升级到最新版本,可以使用 --upgrade 选项和 pip install 命令。
To upgrade a package to the latest version, you can use the --upgrade option with the pip install command.
Listing Installed Packages
您可以使用 pip list 命令列出 Python 环境中的所有已安装包。
You can list all the installed packages in your Python environment using the pip list command.
在处理 Python 项目时,通常需要了解在环境中安装了哪些包和版本。pip 提供了几条命令来列出和管理已安装的包。
When working on Python projects, it is often necessary to know which packages and versions are installed in your environment. pip provides several commands to list and manage installed packages.
To list all installed packages in your current environment, use the following command −
pip list 此命令输出所有已安装包及其各个版本的列表。这对于快速检查您的环境状态非常有用。
pip list This command outputs a list of all installed packages along with their respective versions. This is useful for quickly checking the state of your environment.
Detailed Information
有关每个已安装包的更多详细信息,可以使用 pip show 命令和后跟的包名称 −
For more detailed information about each installed package, you can use the pip show command followed by the package name −
pip show requests
此命令显示有关指定包的详细信息,包括 −
This command displays detailed information about the specified package, including −
-
Name
-
Version
-
Summary
-
Home-page
-
Author
-
Author-email
-
License
-
Location
-
Requires
-
Required-by
Outdated Packages
若要检查环境中的过时包,您可以使用以下命令:
To check for outdated packages in your environment, you can use the following command −
pip list --outdated
此命令将列出所有已安装但有较新版本可用的包。输出包括当前版本和可用的最新版本。
This command lists all installed packages that have newer versions available. The output includes the current version and the latest version available.
Uninstalling Packages
若要卸载某个包,您可以使用 pip uninstall 命令。
To uninstall a package, you can use the pip uninstall command.
当您在环境中不再需要某个 Python 包时,您可以使用 pip 卸载它。以下是卸载包的方法:
When you no longer need a Python package in your environment, you can uninstall it using pip. Here is how you can uninstall packages −
Uninstalling a Single Package
若要卸载单个包,请使用 pip uninstall 命令后跟包名。例如,若要卸载 requests 包:
To uninstall a single package, use the pip uninstall command followed by the package name. For example, to uninstall the requests package −
pip uninstall requests
系统将提示您确认卸载。键入 y ,然后按“Enter”继续。
You will be prompted to confirm the uninstallation. Type y and press "Enter" to proceed.
Freezing Installed Packages
在 Python 中冻结已安装的包是指生成一个列表,其中列出了在您的环境中安装的所有包及其版本。此列表会保存到“requirements.txt”文件中,并且可在其他地方用于重新创建确切的环境。
Freezing installed packages in Python refers to generating a list of all packages installed in your environment along with their versions. This list is saved to a "requirements.txt" file and can be used to recreate the exact environment elsewhere.
Using "pip freeze"
pip freeze 命令将列出所有已安装的包及其版本。您可以使用 shell 重定向 > 运算符将其输出定向到“requirements.txt”文件:
The pip freeze command lists all installed packages and their versions. You can direct its output to a "requirements.txt" file using the shell redirection > operator −
pip freeze > requirements.txt
此命令将使用格式为“package==version”的包和版本列表创建或覆盖“requirements.txt”文件。
This command creates or overwrites "requirements.txt" with a list of packages and versions in the format "package==version".
Using a requirements.txt File
requirements.txt 文件是一种使用 pip 指定要安装的包列表的方法。这对于确保为某个项目安装所有依赖项非常有用。
A requirements.txt file is a way to specify a list of packages to be installed using pip. This is useful for ensuring that all dependencies are installed for a project.
Using Virtual Environments
虚拟环境允许您为不同的项目创建隔离的 Python 环境。这可确保不同项目的依赖项不会相互干扰。
Virtual environments allow you to create isolated Python environments for different projects. This ensures that dependencies for different projects do not interfere with each other.
Creating a Virtual Environment
您可以使用以下命令创建虚拟环境:
You can create a virtual environment using the following command −
python -m venv myenv
将 myenv 替换为您首选的虚拟环境名称。此命令将创建一个名为 myenv(或您指定的名称)的目录,其中包含一个自包含的 Python 环境。
Replace myenv with your preferred name for the virtual environment. This command creates a directory named myenv (or your specified name) containing a self-contained Python environment.
Activating the Virtual Environment
根据您的操作系统,激活虚拟环境:
Depending on your operating system, activate the virtual environment −
-
On Windows −
-
On macOS and Linux −
激活后,命令提示符会更改,显示虚拟环境名称(本例中为 myenv),表示您现在在该环境中工作。
Once activated, your command prompt will change to show the name of the virtual environment (myenv in this case), indicating that you are now working within it.