杰瑞科技汇

Python安装pip的步骤是什么?

Of course! Here’s a comprehensive guide on how to install pip in Python, covering the most common scenarios.

The Short Answer (For Modern Systems)

For Python 3.4 and later, pip is included by default. You just need to make sure it's accessible and up-to-date.

Check if pip is already installed: Open your terminal or command prompt and run:

pip --version
# or for Python 3 specifically
pip3 --version
  • If you see a version number (e.g., pip 23.2.1 from ...), you're all set! You can skip to the "Updating pip" section.
  • If you get an error like 'pip' is not recognized..., continue to the installation steps below.

Detailed Installation Guide

The best method depends on your operating system and how you installed Python.

Method 1: On Linux or macOS (Recommended)

This is the most common and reliable method for Linux and macOS users.

Step 1: Ensure pip is installed with ensurepip

Python's built-in ensurepip module is the safest way to install pip. Run this command in your terminal:

# For Python 3
python3 -m ensurepip --upgrade
  • python3: This explicitly uses your Python 3 interpreter. It's good practice to use python3 instead of just python to avoid conflicts with Python 2.
  • -m ensurepip: This tells Python to run the ensurepip module.
  • --upgrade: This flag ensures that if pip is already installed, it's updated to the latest version.

Step 2: Verify the Installation

Check if pip is now in your system's PATH:

pip3 --version

You should see output similar to this:

pip 23.2.1 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

Method 2: On Windows (Recommended)

Windows doesn't have a direct equivalent of the ensurepip command, but the process is still straightforward.

Step 1: Open the Command Prompt or PowerShell

  • Press the Windows Key.
  • Type cmd or powershell and press Enter.

Step 2: Run the ensurepip Command

Just like on Linux/macOS, you can use the built-in module. The command is slightly different because you don't typically have a python3 executable.

py -m ensurepip --upgrade
  • py: This is the Python launcher for Windows, which is the recommended way to run Python. It automatically finds the correct version of Python installed on your system.

Step 3: Verify the Installation

Check if pip is working:

pip --version

You should see version information.


What if ensurepip Fails? (Alternative Method)

In rare cases, ensurepip might not be available or might fail. The most common alternative is to use the get-pip.py script.

Step 1: Download the get-pip.py script Open your web browser and navigate to the official get-pip page: https://bootstrap.pypa.io/get-pip.py Right-click on the page and select "Save Link As..." to save the file as get-pip.py to your Desktop or another easy-to-find location.

Step 2: Run the script from your terminal Navigate to the directory where you saved the file using cd.

  • On Linux/macOS:

    cd ~/Desktop
    python3 get-pip.py
  • On Windows:

    cd C:\Users\YourUsername\Desktop
    py get-pip.py

This script will download and install pip for you.


Important: Adding Python to PATH

If you installed Python from the official installer on Windows, you should have checked a box that says "Add Python to PATH". If you didn't, you will have to use the Python launcher (py) every time.

To check if Python is in your PATH:

  1. Open Command Prompt.
  2. Type python --version or pip --version.
  3. If it works, you're good.
  4. If it doesn't, you need to manually add Python to your PATH environment variable.

How to Update pip

It's a very good practice to keep pip updated to the latest version. You can do this with a simple command.

# The recommended way to update pip
python3 -m pip install --upgrade pip
# or on Windows
py -m pip install --upgrade pip

Using python3 -m pip or py -m pip is the most reliable method, as it ensures you are updating the pip associated with that specific Python installation.

Summary

Situation Command
Linux/macOS (First-Time Install) python3 -m ensurepip --upgrade
Windows (First-Time Install) py -m ensurepip --upgrade
Updating pip (Cross-Platform) python3 -m pip install --upgrade pip
Alternative Install Method Download get-pip.py and run python3 get-pip.py

After following these steps, you will have a working and up-to-date pip ready to install Python packages from the Python Package Index (PyPI).

分享:
扫描分享到社交APP
上一篇
下一篇