杰瑞科技汇

Python 3.4在Ubuntu下如何安装配置?

Of course. Here is a comprehensive guide for working with Python 3.4 on Ubuntu.

Python 3.4在Ubuntu下如何安装配置?-图1
(图片来源网络,侵删)

Important Context: Python 3.4 is End-of-Life

First and foremost, it's crucial to understand that Python 3.4 reached its official end-of-life on March 16, 2025. This means:

  • No Security Updates: It no longer receives any security patches. Running it on a server or any system connected to the internet is a significant security risk.
  • No Bug Fixes: Critical bugs that are discovered will not be fixed.
  • No New Features: It will not receive any new features or improvements.
  • Not Supported by Major Libraries: Many modern Python libraries (like numpy, pandas, Django, etc.) have dropped support for Python 3.4 and require newer versions.

You should only use Python 3.4 for legacy projects where you cannot upgrade the codebase or for educational purposes to understand older systems.


Checking if Python 3.4 is Already Installed

Modern versions of Ubuntu (18.04, 20.04, 22.04, etc.) do not come with Python 3.4 pre-installed. However, you can check if it's available in the repositories or if someone has already installed it manually.

Open your terminal and run:

Python 3.4在Ubuntu下如何安装配置?-图2
(图片来源网络,侵删)
# Check for the version in the repositories
apt-cache policy python3.4
# Check if it's installed on the system
python3.4 --version

If the apt-cache command shows a version number, it means it's available in the Ubuntu repositories. If python3.4 --version returns a version, it's already installed.


Installing Python 3.4 from Ubuntu Repositories

This is the recommended method if you absolutely must have Python 3.4. It will be an older, unpatched version, but it integrates well with the system's package manager.

For Ubuntu 16.04 (Xenial Xerus) or Older

Python 3.4 is in the default repositories for Ubuntu 16.04.

sudo apt-get update
sudo apt-get install python3.4

For Ubuntu 18.04 (Bionic Beaver) and Newer

Python 3.4 was removed from the default repositories for Ubuntu 18.04 and later. However, it's still available in the "universe" repository, which is enabled by default on most Ubuntu installations.

Python 3.4在Ubuntu下如何安装配置?-图3
(图片来源网络,侵删)
# Update your package lists
sudo apt-get update
# Install Python 3.4 and its standard library
sudo apt-get install python3.4
# It's also highly recommended to install pip and venv for Python 3.4
sudo apt-get install python3.4-venv python3.4-pip

Using pyenv (The Recommended Modern Approach)

A much better way to manage multiple Python versions on a single machine is with a tool like pyenv. It allows you to install and switch between different Python versions without interfering with the system's default Python.

This is the preferred method for developers.

Step 1: Install Dependencies

First, install the necessary dependencies to build Python from source.

sudo apt-get update
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Step 2: Install pyenv

Follow the official installation script. It's the safest and easiest way.

curl https://pyenv.run | bash

Step 3: Configure Your Shell

pyenv works by modifying your shell's environment. You need to add it to your shell's startup file. For Bash (the default on Ubuntu), edit ~/.bashrc.

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

Now, restart your shell for the changes to take effect. You can either open a new terminal or run source ~/.bashrc.

Step 4: Install Python 3.4 with pyenv

Now you can install any Python version you want. pyenv will download it from source and compile it.

# Install Python 3.4. This may take a few minutes
pyenv install 3.4.10
# Verify that it was installed correctly
pyenv versions
# You should see 3.4.10 listed

Step 5: Using Python 3.4

pyenv gives you two main ways to use a specific version.

A) Set a Global Version (for your user account)

This will make python3 point to Python 3.4 in all your new terminals.

pyenv global 3.4.10
# Check the version
python --version
# Should output: Python 3.4.10

B) Set a Local Version (for a specific project)

This is the best practice. Navigate to your project directory and set the local version. This only affects that directory.

cd /path/to/your/project
pyenv local 3.4.10
# Check the version
python --version
# Should output: Python 3.4.10
# You can see the setting in a .python-version file
cat .python-version
# Should contain: 3.4.10

To switch back to the system's default Python, you can either run pyenv local --unset or set a different version.


Common Tasks with Python 3.4

Creating a Virtual Environment

It is critical to use a virtual environment to isolate your project's dependencies. This prevents conflicts between different projects.

If you installed python3.4-venv:

python3.4 -m venv my_project_env
source my_project_env/bin/activate

If you are using pyenv, the virtual environment will use the Python version that pyenv is currently pointing to.

# Make sure pyenv is set to 3.4.10
pyenv local 3.4.10
# Create and activate the venv
python -m venv my_project_env
source my_project_env/bin/activate

Your command prompt will change to show that the environment is active, e.g., (my_project_env) user@host:~$.

Installing Packages with pip

Inside your active virtual environment, use pip to install packages.

# Upgrade pip first (a good practice)
python -m pip install --upgrade pip
# Install a package
pip install requests==2.9.0  # Example: install an old version of requests

Be mindful that you can only install versions of packages that are compatible with Python 3.4.

Summary and Final Recommendation

Method Pros Cons Best For
Ubuntu Repositories Simple, integrates with apt. Old, unpatched version, conflicts with system tools. Quick tests on older systems (Ubuntu 16.04).
pyenv Recommended. Isolates versions, safe, can install any version, doesn't break system Python. Requires initial setup, builds from source (slower). Developers who need to work on multiple projects with different Python versions.

Final Advice: Unless you are working on a legacy system frozen at Ubuntu 16.04, use pyenv. It is the modern, safe, and professional way to manage Python versions and will save you from many headaches in the long run.

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