杰瑞科技汇

Python3.4 for Linux如何安装配置?

Of course. Here is a comprehensive guide to Python 3.4 on Linux, covering its history, installation, usage, and most importantly, how to manage it alongside modern Python versions.

Python3.4 for Linux如何安装配置?-图1
(图片来源网络,侵删)

Important: The Status of Python 3.4

First and foremost, it is crucial to understand that Python 3.4 reached its official End-of-Life (EOL) on March 16, 2025.

This means:

  • No Security Updates: It no longer receives any security patches. Running it in production or on a networked machine is a significant security risk.
  • No Bug Fixes: Any bugs discovered will not be fixed.
  • No Standard Library Updates: It does not have the features, improvements, or bug fixes found in newer Python versions.
  • Not Recommended for New Projects: You should not start any new development in Python 3.4.

Why would you still need it? The primary reasons for needing Python 3.4 today are:

  • Legacy Systems: Maintaining very old applications or services that were built specifically for Python 3.4 and are too costly or difficult to upgrade.
  • Specific Library Dependencies: A third-party library you absolutely must use has not been updated to work with Python 3.5+ and only supports 3.4.

Installation Methods

There are three main ways to get Python 3.4 on a modern Linux system. The best method depends on your needs (e.g., temporary use vs. long-term legacy maintenance).

Python3.4 for Linux如何安装配置?-图2
(图片来源网络,侵删)

Method 1: Using a Package Manager (Easiest but Limited)

This is the simplest method, but it's only available on older Linux distributions or specific repositories.

For Debian / Ubuntu (and derivatives): Older versions of Ubuntu (like 14.04 "Trusty Tahr") and Debian (like "Jessie") had Python 3.4 in their main repositories. On modern systems, you may need to use the "universe" repository or a deadsnake PPA.

# On modern Ubuntu/Debian, you might need the deadsnake PPA
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
# Install Python 3.4
sudo apt install python3.4

Note: This will also likely install python3.4-venv, but pip for 3.4 (pip3.4) might need to be installed separately or may not be available from the official repos. You'll often have to use ensurepip to set it up.

For Fedora / CentOS / RHEL: These systems are more conservative with Python versions. You will almost certainly need the Software Collections (SCL) repository, which provides newer versions of software without conflicting with the system's default versions.

Python3.4 for Linux如何安装配置?-图3
(图片来源网络,侵删)
# Enable the EPEL and SCL repositories
sudo yum install centos-release-scl epel-release
# Install Python 3.4 from the SCL
sudo yum install rh-python34
# To use this version, you need to "enable" it in your shell session
scl enable rh-python34 bash

After running scl enable, your command prompt will change, and python and pip will now point to the Python 3.4 versions.

Method 2: Compiling from Source (Most Control)

This method gives you the most flexibility and is the recommended approach for long-term maintenance of a legacy project. It installs Python 3.4 alongside your system's default Python without interfering with it.

  1. Install Dependencies: You need the development tools and libraries required to build Python.

    # For Debian/Ubuntu
    sudo apt update
    sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
    # For Fedora/CentOS/RHEL
    sudo yum groupinstall "Development Tools"
    sudo yum install zlib-devel ncurses-devel gdbm-devel nss-devel openssl-devel readline-devel libffi-devel sqlite-devel bzip2-devel wget
  2. Download the Source Code: Go to the Python 3.4 download page and get the source tarball.

    cd /tmp
    wget https://www.python.org/ftp/python/3.4.10/Python-3.4.10.tgz
  3. Compile and Install: It's a best practice to install Python into a directory like /usr/local/python3.4 to keep it organized.

    tar -xzf Python-3.4.10.tgz
    cd Python-3.4.10
    # Configure the build. The --prefix flag specifies the installation directory.
    # The --enable-optimizations flag builds a more optimized version.
    ./configure --prefix=/usr/local/python3.4 --enable-optimizations
    # Compile the source code (this can take a few minutes)
    make -j 8  # Use -j with the number of CPU cores for faster compilation
    # Install the compiled binaries
    sudo make altinstall  # Use 'altinstall' instead of 'install' to avoid overwriting the system 'python' command
  4. Verify the Installation:

    /usr/local/python3.4/bin/python3.4 --version
    # Should output: Python 3.4.10
    /usr/local/python3.4/bin/pip3.4 --version
    # Should output the pip version for Python 3.4

Method 3: Using a Version Manager (Recommended for Developers)

A tool like pyenv is the best way to manage multiple Python versions on a single machine. It allows you to install and switch between Python versions (like 3.4, 3.8, 3.11) on a per-project basis.

  1. Install pyenv: Follow the official instructions on the pyenv GitHub page. A common method is using a shell script:

    curl https://pyenv.run | bash

    This will install pyenv and guide you through adding it to your shell's configuration file (.bashrc, .zshrc, etc.).

  2. Install Python 3.4 using pyenv:

    # Update pyenv to know about all available versions
    pyenv update
    # Install Python 3.4.10 (or the latest 3.4.x patch)
    pyenv install 3.4.10
  3. Use Python 3.4:

    # Set the global Python version to 3.4 (not recommended for most users)
    pyenv global 3.4.10
    # OR, set it for the current directory only (better practice)
    cd /path/to/your/legacy/project
    pyenv local 3.4.10
    # Now, just type 'python' or 'pip' in this directory
    python --version
    # Should output: Python 3.4.10
    pip install --upgrade pip

Post-Installation: Setting Up pip and Virtual Environments

For any serious work, you must use pip and virtual environments.

Setting up pip for Python 3.4

If your installation method didn't provide pip3.4, you can bootstrap it using the built-in ensurepip module.

# If you installed from source, the path will be different
python3.4 -m ensurepip --default-pip

This will install pip and setuptools for your Python 3.4 installation.

Creating a Virtual Environment (CRITICAL)

A virtual environment isolates your project's dependencies from the global system and other projects. This is non-negotiable for avoiding dependency conflicts.

# If you used the source install method
/usr/local/python3.4/bin/python3.4 -m venv my-project-env
# If you used pyenv
python -m venv my-project-env  # 'python' points to 3.4 in this directory
# If you used a package manager and have python3.4 and pip3.4 in your PATH
python3.4 -m venv my-project-env

To activate the environment:

source my-project-env/bin/activate

Your shell prompt will change to show the active environment, e.g., (my-project-env) user@host:~$.

To use it: Now, python and pip will automatically use the versions inside the environment.

(my-project-env) $ python --version
Python 3.4.10
(my-project-env) $ pip install "some-legacy-library==1.2.3"

To deactivate the environment:

(my-project-env) $ deactivate
user@host:~$

Key Features and Differences in Python 3.4

Understanding what was new and what was missing can help you debug legacy code.

New in 3.4:

  • asyncio Module: A foundational library for writing concurrent code using async and await syntax (though the syntax itself came in Python 3.5, the core asyncio module was introduced in 3.4).
  • Pathlib Module: A modern, object-oriented way to handle filesystem paths (pathlib.Path), an improvement over the older os.path.
  • Enum Type: A formal way to create enumeration classes.
  • __pycache__ Directories: Replaced .pyc files in directories, making bytecode caching cleaner.
  • typing Module: The first step towards adding type hints to Python, although it was very limited compared to modern versions.

Missing Compared to Modern Python (3.5+):

  • f-strings: Formatted string literals (f"Hello, {name}") were introduced in Python 3.5. In 3.4, you had to use .format() or -formatting.
  • async/await Keywords: The syntax for asyncio was clunkier in 3.4, using @asyncio.coroutine decorators and yield from.
  • dataclasses: The elegant @dataclass decorator is from Python 3.7.
  • Union Types (): The int | str syntax for type hints is from Python 3.10. In 3.4, you had to use Union[int, str] from the typing module.
  • Dictionary Merging (dict1 | dict2): This operator is also from Python 3.10.
  • secrets Module: The recommended module for generating cryptographically strong random numbers is from Python 3.6. In 3.4, you might have used os.urandom() or the random module (which is not suitable for security).

Summary and Recommendation

Method Pros Cons Best For
Package Manager Very easy, integrates with system updates. May not be available, insecure EOL packages. Quick tests on old, unsupported distros.
Compile from Source Full control, secure, doesn't conflict with system Python. More complex, requires build dependencies. Long-term maintenance of legacy systems/servers.
pyenv (Version Manager) Isolates versions, easy to switch, great for developers. Adds a small layer of complexity. Almost all developer use cases.

Final Recommendation: If you are a developer needing to work with Python 3.4 for a legacy project, use pyenv. It is the cleanest, safest, and most flexible way to manage your Python environment. If you are a system administrator maintaining a server, compiling from source into a custom directory like /usr/local/python3.4 is the most robust and secure approach.

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