Python不仅仅是一种适合初学者的编程语言——它是一种功能强大且成熟的语言,围绕它创建了大量的第三方包和库。您可以部署这些第三方包并将它们用作您自己的编码项目的一部分,而不是自己编写所有代码。
您可以通过搜索 GitHub(searching GitHub)或 Web 找到这些,但您也可以使用PIP ( Python的包管理器)来代替。PIP允许您从Python 包索引(Python Package Index)中搜索和安装Python PIP包,有数千个包可用。
什么是画中画?(What Is PIP?)
PIP 或P ython 的P ackage安装程序允许您从Python 包索引(Python Package Index)中(I)可用的Python包的中央存储库安装包。简单来说,它充当Python的应用商店,允许您从命令行或终端安装Python包,而无需自己定位它们。(Python)
Linux用户已经熟悉这个过程——包管理器是Linux用户体验的重要组成部分,APT、Pacman和其他允许您从终端窗口在各种Linux发行版上安装不同的软件。(Linux)
在开始使用PIP之前,您需要安装Python。macOS 和大多数Linux发行版已经安装了Python,但您需要在Windows 10上手动安装它。如果您的Linux发行版没有安装Python(或具有较旧版本的Python),您还需要使用发行版的包管理器执行此操作。
在 Linux 上安装 Python PIP(Installing Python PIP On Linux)
python-pip软件包可用于所有主要Linux发行版的下载和安装,以及变体。根据您的Linux发行版,如果您仍在使用Python 2.x系列而不是Python 3.x系列,则可能需要安装python2-pip包。
如果您使用的是Python 3.4或更高版本(或 Python 2.7.9 或更高版本),则无需在Linux上安装(Linux)PIP,因为它已经安装。通过打开终端窗口并输入python3 –version来检查您安装的Python版本,然后使用python -m pip或python3 -m pip来使用它。
对于低于 2.7.9 或 3.4的Python版本,在Linux上安装(Linux)Python PIP将取决于使用的系统包管理器。以下是如何在各种Linux平台上安装PIP 。
- 要在Ubuntu或基于 Debian 的发行版上安装Python PIP,请打开终端并输入sudo apt install python-pip for Python 2.x。键入sudo apt install python3-pip以安装Python 3.x。
- 在Arch Linux 上(Arch Linux),打开一个终端并为Python 2.x键入(Python 2.x.)sudo pacman -S python2-pip 。键入sudo pacman -S python-pip以安装Python 3.x。
- 如果您使用的是基于 Fedora 的发行版,请输入sudo dnf upgrade python-setuptools,然后输入sudo dnf install python-pip python-wheel for Python 2.x。键入sudo dnf install python3 python3-wheel以安装Python 3.x。
- 要手动安装PIP,请打开终端并输入curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py。下载后,键入python get-pip.py以运行自动安装脚本。
在 Windows 上安装 Python PIP(Installing Python PIP On Windows)
与Linux不同,Windows没有预装Python 。这意味着您需要先下载并安装适用于 Windows 的 Python,(download and install Python for Windows)然后才能开始在此平台上使用PIP 。
如果您安装了最新版本的Python,那么您应该会发现PIP与您的Python安装一起安装。正如我们所提到的,Python 2.7.9和Python 3.4安装(及更高版本)应该自动安装PIP。
如果您使用的是较旧的Python版本,则需要使用get-pip脚本自动安装 pip。
- 将 get-pip.py 脚本下载(Download the get-pip.py script)到您的Windows PC。在具有管理访问权限的提升的PowerShell或命令行中,键入python get-pip.py以自动安装 PIP。
这应该会自动安装Python PIP供您使用。然后,您可以通过键入pip或python -m pip从(python -m pip)PowerShell或命令行窗口运行它。
如何在 macOS 上安装 Python PIP
与Linux一样,macOS通常会安装Python以及一个PIP版本。与最新版本相比,macOS 上的 Python 和 PIP 都可能已过时,但通过打开macOS终端窗口(Python)并键入(PIP)pip或(macOS)python -m(pip) pip ((python -m pip)或pip3或python3 -m pip),它应该可以正常工作。
如果您想同时更新Python和PIP,您可以使用适用于 macOS的Homebrew 包管理器轻松完成此操作。(Homebrew package manager)
- 如果您安装了Homebrew,请打开终端窗口并键入brew install python以将您的安装更新到最新版本的Python 3.x。要安装较旧的Python 2.x安装,请改为键入brew install python@2
安装后,在教程中键入pip或python -m pip(或pip3或python3 -m pip)以运行PIP并查看可用PIP标志的列表。
更新画中画(Updating PIP)
如果您已经安装了PIP,但您想将其更新到最新版本,您可以使用PIP本身来完成。
- 对于 macOS 和 Linux 用户,键入pip install -U pip开始更新过程。您也可以改用pip3 install -U pip或python -m pip install -U pip。
- Windows 用户可以通过打开具有管理员访问权限的PowerShell或命令行并键入python -m pip install -U pip来安装(python -m pip install -U pip)Python PIP。
使用 PIP 安装和卸载 Python 包(Installing & Uninstalling Python Packages Using PIP)
在您选择的平台上安装、更新并准备好使用PIP后(PIP),您终于可以开始使用它来下载和安装Python包,或使用它来卸载现有的包。
- 要使用PIP安装新的Python包,请键入pip install package或python -m pip install package,将package替换为包的名称。如果您同时安装了Python 2.x(Python 2.x)和Python 3.x ,您可能需要在 macOS 等特定平台上使用pip3或python3。
- 要查看所有已安装Python包的列表,请键入pip list或python -m pip list。在某些平台上对Python 3.x包使用pip3或python3 。
- 要删除包,请键入pip uninstall package或python -m pip uninstall package,将package替换为包名称。如果您同时安装了Python 2.x和 3.x ,请在某些平台上使用pip3或python3 。按y确认卸载。
使用 PIP 开发更好的 Python 项目(Using PIP To Develop Better Python Projects)
一旦您知道如何安装Python PIP并使用它来安装新包,您就可以立即访问无数的库和其他项目,这将使您能够使用Python开发更大更好的自己的项目。
Python 是最值得学习的编程语言(best programming languages to learn)之一,尤其是对于新程序员而言,这要归功于其易于理解的语法和为初学者提供的无穷无尽的资源。
无论您是Python新手还是专业人士,总有一些新东西要学习,所以请在下面的评论部分告诉我们您自己的Python技巧(或问题)。
How To Install Python PIP For Python Packages
Python isn’t just a programming language for beginners—it’s a powerful and fully-fledged language, with an enormous number оf third-party packages and libraries created around it. Rather than code everything yourself, you can deploy these third-party packages and use them as part of your own coding projects.
You might find these by searching GitHub or the web, but you can also use PIP, the package manager for Python, to do this instead. PIP allows you to search for and install Python PIP packages from the Python Package Index, with thousands of packages available.
What Is PIP?
PIP, or Package Installer for Python, allows you to install packages from the central repository of Python packages available at the Python Package Index. In simple terms, it acts as an app store for Python, allowing you to install Python packages from a command line or terminal without needing to locate them yourself.
Linux users will already be familiar with this process—package managers are an essential part of the Linux user experience, with APT, Pacman, and others allowing you to install different software on various Linux distributions from a terminal window.
You’ll need Python installed before you can begin to use PIP. macOS and most Linux distributions will already have Python installed, but you’ll need to install it manually on Windows 10. You’ll also need to do this if your Linux distribution doesn’t have Python installed (or has an older version of Python) using the package manager for your distribution.
Installing Python PIP On Linux
The python-pip package is available for download and installation for all major Linux distributions, as are variants. Depending on your Linux distribution, you may need to install the python2-pip package if you’re still using the Python 2.x series, rather than the Python 3.x series.
If you’re using Python 3.4 or above (or Python 2.7.9 or above), you don’t need to install PIP on Linux, as it’s already installed. Check this by opening a terminal window and typing python3 –version to check your installed Python version, then use python -m pip or python3 -m pip to use it.
For Python versions below 2.7.9 or 3.4, installing Python PIP on Linux will depend on the system package manager in use. Here’s how to install PIP on various Linux platforms.
- To install Python PIP on Ubuntu or Debian-based distributions, open a terminal and type sudo apt install python-pip for Python 2.x. Type sudo apt install python3-pip for Python 3.x installations.
- On Arch Linux, open a terminal and type sudo pacman -S python2-pip for Python 2.x. Type sudo pacman -S python-pip for Python 3.x installations.
- If you’re using a Fedora-based distribution, type sudo dnf upgrade python-setuptools, followed by sudo dnf install python-pip python-wheel for Python 2.x. Type sudo dnf install python3 python3-wheel for Python 3.x installations.
- To install PIP manually, open a terminal and type curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py. Once downloaded, type python get-pip.py to run the automatic installation script.
Installing Python PIP On Windows
Unlike Linux, Windows doesn’t come with Python pre-installed. That means you’ll need to download and install Python for Windows first before you can begin to use PIP on this platform.
If you’ve installed the most up-to-date version of Python, then you should find PIP installed alongside your Python installation. As we’ve mentioned, Python 2.7.9 and Python 3.4 installations (and above) should have PIP installed automatically.
If you’re using an older Python version, you’ll need to use the get-pip script to install pip automatically.
- Download the get-pip.py script to your Windows PC. In an elevated PowerShell or command line with administrative access, type python get-pip.py to automatically install PIP.
This should automatically install Python PIP for you to use. You can then run it from a PowerShell or command line window by typing pip or python -m pip.
How To Install Python PIP On macOS
Like Linux, macOS usually comes with Python installed, as well as a version of PIP. Both Python and PIP on macOS are likely to be outdated, compared to the most recent release, but it should work as normal by opening a macOS terminal window and typing pip or python -m pip (or pip3 or python3 -m pip).
If you want to update Python and PIP together, you can do this easily using the Homebrew package manager for macOS.
- If you have Homebrew installed, open a terminal window and type brew install python to update your installation to the latest version of Python 3.x. To install an older Python 2.x installation, type brew install python@2 instead.
Once it’s installed, type pip or python -m pip (or pip3 or python3 -m pip) at the tutorial to run PIP and see a list of available PIP flags.
Updating PIP
If you already have PIP installed, but you want to update it to the latest version, you can do that using PIP itself.
- For macOS and Linux users, type pip install -U pip to begin the update process. You can also use pip3 install -U pip or python -m pip install -U pip instead.
- Windows users can install Python PIP by opening up a PowerShell or command line with admin access and typing python -m pip install -U pip.
Installing & Uninstalling Python Packages Using PIP
Once PIP is installed, updated, and ready to use on your chosen platform, you can finally begin to use it to download and install Python packages, or use it to uninstall existing packages.
- To install a new Python package using PIP, type pip install package or python -m pip install package, replacing package with the name of the package. You may need to use pip3 or python3 on certain platforms like macOS if you have both Python 2.x and Python 3.x installed.
- To see a list of all installed Python packages, type pip list or python -m pip list. Use pip3 or python3 for Python 3.x packages on certain platforms.
- To remove a package, type pip uninstall package or python -m pip uninstall package, replacing package with the package name. Use pip3 or python3 on certain platforms if you have both Python 2.x and 3.x installed. Press y to confirm the uninstallation.
Using PIP To Develop Better Python Projects
Once you know how to install Python PIP and use it to install new packages, you immediately have access to an endless number of libraries and other projects that will allow you to develop bigger and better projects of your own using Python.
Python is one of the best programming languages to learn, especially for new coders, thanks to an easy-to-understand syntax and an endless number of resources for beginners.
Whether you’re a new Python coder or a pro, there’s always something new to learn, so let us know your own Python tips (or questions) in the comments section below.