杰瑞科技汇

Python2与Python3的pip该如何区分使用?

Of course! This is a very common point of confusion for people starting with Python. Let's break down Python 2, Python 3, and pip in detail.

Python2与Python3的pip该如何区分使用?-图1
(图片来源网络,侵删)

Python 2 vs. Python 3: The Core Difference

This is the fundamental "why" behind having two versions.

Python 2 (Legacy)

  • Status: End-of-Life (EOL) as of January 1, 2025. This means it no longer receives updates, including critical security patches.
  • Philosophy: It was designed for simplicity and compatibility with older code. Some design decisions were later seen as problematic.
  • Key Syntax Differences:
    • print statement: print "Hello, World!" (It was a keyword, not a function).
    • Integer Division: 3 / 2 results in 1. (True division was handled by a special float type).
    • Unicode: Strings were byte-strings by default. Unicode support was clunky.

Python 3 (Present & Future)

  • Status: The current, actively developed, and future of the language.
  • Philosophy: Designed to fix the ambiguities and inconsistencies of Python 2, making the language cleaner, more consistent, and more powerful.
  • Key Syntax Changes (that "break" Python 2 code):
    • print() function: print("Hello, World!") (It's now a built-in function).
    • True Division: 3 / 2 correctly results in 5. Integer division is now done with : 3 // 2 results in 1.
    • Unicode: Strings are Unicode by default (str type), and byte-strings are a separate type (bytes type). This makes internationalization much easier.

The Analogy: Think of Python 2 as an old, reliable car that is no longer manufactured or serviced. You can still drive it, but it's unsafe and won't get new features. Python 3 is a brand-new, modern car with a better engine, safety features, and is designed for the roads of the future.


pip: The Package Installer

Now, let's talk about pip.

What is pip?

pip is the de facto standard package manager for Python. Its name is a recursive acronym: Pip Installs Packages.

Python2与Python3的pip该如何区分使用?-图2
(图片来源网络,侵删)
  • What it does: It allows you to easily download, install, and manage third-party libraries (packages) from the Python Package Index (PyPI).
  • Why you need it: The standard Python library is powerful, but it doesn't have everything. If you want to use a web framework like Django, do scientific computing with NumPy, or work with APIs using requests, you will use pip to install them.

pip and Python 2 vs. Python 3

This is the crucial part. You don't just have "one" pip. You have a pip for each Python version you have installed.

  • pip for Python 2: This pip installs packages that can only be used with a Python 2 interpreter. It's usually named pip.
  • pip for Python 3: This pip installs packages for a Python 3 interpreter. It's usually named pip3.

How to use them:

# Installs a package for Python 3
pip3 install requests
# Installs a package for Python 2 (if you still have it)
pip install requests

On Linux/macOS, you might also see python -m pip: This is often the most reliable way to use pip because it explicitly tells Python to run the pip module associated with that specific Python interpreter.

# Use pip associated with your default python3
python3 -m pip install pandas
# Use pip associated with your default python2 (if it exists)
python2 -m pip install some-old-package

The Installation and Workflow

Here’s a typical modern workflow for a developer.

Python2与Python3的pip该如何区分使用?-图3
(图片来源网络,侵删)

Step 1: Installation

macOS (using Homebrew - Recommended): This is the easiest way to get a clean, separate installation of Python 3.

# Install Python 3. Homebrew creates a 'python3' command and 'pip3'
brew install python

Windows: Download the official installer from python.org. Crucially, during installation, check the box that says "Add Python to PATH". This will automatically set up python and pip commands.

Linux (Debian/Ubuntu):

# This installs both python3 and the pip3 command
sudo apt update
sudo apt install python3 python3-pip

Step 2: The Modern Workflow

For any new project in 2025 and beyond, you should only use Python 3.

  1. Create a Project Directory:

    mkdir my_new_project
    cd my_new_project
  2. Create a Virtual Environment (Highly Recommended!): A virtual environment creates an isolated space for your project's dependencies. This prevents package version conflicts between projects.

    # Create a virtual environment named 'venv'
    python3 -m venv venv
  3. Activate the Virtual Environment:

    # On macOS/Linux
    source venv/bin/activate
    # On Windows
    .\venv\Scripts\activate

    Your command prompt will change to show (venv), indicating the environment is active.

  4. Install Packages with pip: Now, the pip command you use will automatically be the one inside your venv, which is linked to your Python 3 installation. You don't need to type pip3.

    # Installs the 'requests' library into your project's isolated environment
    pip install requests
  5. Write Your Code: Write your Python code using Python 3 syntax. For example, in app.py:

    # app.py
    import requests
    response = requests.get('https://api.github.com')
    print(f"Status Code: {response.status_code}")
    print(f"Response uses Python 3's f-strings!")
  6. Run Your Code:

    # The 'python' command now points to the python3 inside your venv
    python app.py
  7. Deactivate the Environment: When you're done working on the project:

    deactivate

Summary Table

Feature Python 2 Python 3 pip
Status End-of-Life (EOL) Active & Future The package manager
print print "hello" print("hello") -
Division 3/2 -> 1 3/2 -> 5 -
Strings str is bytes str is Unicode -
Command python python3 -
pip for it pip or pip2 pip3 pip install <package>
Recommendation Do not use for new projects. Use for all new projects. Use pip3 or python3 -m pip

Final Recommendation

  1. For any new project, use Python 3.
  2. Use a virtual environment (venv) for every project.
  3. Use pip (which will be linked to Python 3 inside your venv) to install packages.
  4. Learn to migrate old Python 2 code to Python 3 if you encounter it. The 2to3 tool can help automate much of this.
分享:
扫描分享到社交APP
上一篇
下一篇