杰瑞科技汇

为何仍要安装已停止维护的Python 2.7?

Of course. Here is a comprehensive guide on how to install Python 2.7, covering the most common operating systems.

为何仍要安装已停止维护的Python 2.7?-图1
(图片来源网络,侵删)

⚠️ Very Important: Why You Probably Shouldn't Use Python 2.7

Before you proceed, it's crucial to understand that Python 2.7 reached its official "end-of-life" on January 1, 2025.

This means:

  • No Security Updates: Any code you write or run in Python 2.7 is vulnerable to security exploits that will never be fixed.
  • No Bug Fixes: Any lingering bugs will remain unfixed.
  • No New Features: You will miss out on decades of improvements, performance enhancements, and modern language features available in Python 3.
  • Most Libraries Have Dropped Support: The vast majority of popular Python libraries (like NumPy, Pandas, Django, etc.) have stopped supporting Python 2.7 and only release versions for Python 3.

You should only install Python 2.7 if you absolutely must:

  • To maintain or run a legacy application that has not yet been ported to Python 3.
  • To work with a specific library or system that is frozen on Python 2.7.

If you are learning Python for the first time or starting a new project, please use the latest version of Python 3.

为何仍要安装已停止维护的Python 2.7?-图2
(图片来源网络,侵删)

Installation Guides

Choose the guide that matches your operating system.


Installation on Linux (Debian/Ubuntu)

On modern Debian or Ubuntu systems, python2.7 is often available as a package from the repositories, but it may not be the default python command.

Step 1: Update Your Package List

Open a terminal and run:

sudo apt update

Step 2: Install Python 2.7

sudo apt install python2.7

Step 3: Verify the Installation

Check the version to confirm it's installed:

python2.7 --version

You should see output like Python 2.7.18.

Step 4: (Optional) Install pip for Python 2.7

pip is the package installer for Python. You'll likely need it to install libraries. The package is usually called python2.7-pip.

sudo apt install python2.7-pip

To use it, you must call it explicitly:

pip2.7 install <package-name>

Installation on macOS

macOS used to come with Python 2.7 pre-installed, but recent versions (especially after macOS 12.5.1) have removed it. The best way to install it now is using a version manager like pyenv, which is highly recommended.

Method A: Using pyenv (Recommended)

This method allows you to install and manage multiple Python versions without conflicts.

Step 1: Install pyenv If you don't have Homebrew, install it first:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Then install pyenv using Homebrew:

brew install pyenv

Step 2: Configure Your Shell You need to add pyenv to your shell's path. Open your shell configuration file (~/.zshrc for Zsh, the default on modern macOS, or ~/.bash_profile for Bash) and add these lines:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

Then restart your terminal or run source ~/.zshrc.

Step 3: Install Python 2.7 Now you can install any version of Python easily:

pyenv install 2.7.18

Step 4: Use Python 2.7 You can now use Python 2.7 in two ways:

  1. Globally (for your current user): pyenv global 2.7.18
  2. Locally (for the current directory only): pyenv local 2.7.18 (This creates a .python-version file)

To verify, run python --version. It should now show Python 2.7.18.

Method B: Direct Download (Simpler but less flexible)

  1. Go to the official Python 2.7 download page: https://www.python.org/downloads/release/python-2718/
  2. Download the "macOS 64-bit installer".
  3. Run the downloaded .pkg file and follow the on-screen instructions.

Installation on Windows

The official Python website provides an installer for Windows.

Step 1: Download the Installer

Go to the Python 2.7.18 download page: https://www.python.org/downloads/release/python-2718/ Under the "Files" section, find and download the Windows x86-64 installer (if you have a 64-bit system) or the Windows x86 installer (if you have a 32-bit system). The filename will be something like python-2.7.18.amd64.msi.

Step 2: Run the Installer

Double-click the .msi file to launch the installer.

Step 3: Configure the Installation (Crucial!)

In the installer window, make sure to check the box at the bottom that says "Add python.exe to Path". This will make it much easier to run Python from the command line.

If you miss this step, you will have to manually add Python to your system's PATH environment variable, which is more complicated.

Step 4: Complete the Installation

Click "Install Now" and follow the prompts. The installer may take a few minutes to complete.

Step 5: Verify the Installation

Open Command Prompt or PowerShell and type:

python --version

or

python -V

You should see Python 2.7.18.

If you get an error like 'python' is not recognized..., it means the "Add python.exe to Path" option failed. You will need to manually add Python to your PATH.

Step 6: (Optional) Install pip

Python 2.7.18 and later installers usually include pip. To check if it's installed, run:

pip --version

If it's not there, you can download get-pip.py and run it with Python 2.7:

  1. Download get-pip.py from https://bootstrap.pypa.io/pip/2.7/
  2. Save it to a location like C:\.
  3. Open Command Prompt and run:
    python C:\get-pip.py

Post-Installation: Creating an Alias for Convenience

On Linux and macOS, typing python2.7 every time can be tedious. You can create an alias.

For Linux (Bash): Add this line to your ~/.bashrc or ~/.bash_profile file:

alias python=python2.7

Then run source ~/.bashrc or open a new terminal.

For macOS (Zsh): Add this line to your ~/.zshrc file:

alias python=python2.7

Then run source ~/.zshrc or open a new terminal.

Warning: Creating this alias can cause issues, as some system tools expect python to point to a modern Python 3. Use this alias with caution and only if you are sure you need it for your workflow.

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