杰瑞科技汇

Ubuntu 17.10如何配置Python环境?

Of course. Here's a comprehensive guide to using Python on Ubuntu 17.10, covering its default state, recommended upgrades, and best practices for development.

Ubuntu 17.10如何配置Python环境?-图1
(图片来源网络,侵删)

Quick Summary

Ubuntu 17.10 is quite old and reached its End of Life (EOL) in July 2025. The single most important recommendation is to upgrade to a modern, supported version of Ubuntu.

However, if you must use 17.10, here's what you need to know:

  • Default Python: Python 3.6
  • Default Python 2: Python 2.7.14
  • Recommendation: Use the system's python3 command for scripts and projects. Avoid using python as it points to Python 2 by default.
  • Best Practice: Use a virtual environment (venv) for every project to manage dependencies.

Checking Your Python Installation on Ubuntu 17.10

First, let's see what Python versions are installed on your system. Open a terminal (Ctrl+Alt+T) and run the following commands:

Check for Python 3

This is the recommended version for all new development.

Ubuntu 17.10如何配置Python环境?-图2
(图片来源网络,侵删)
python3 --version

Expected Output:

Python 3.6.6

Check for Python 2

Python 2 is legacy and should only be used for maintaining old projects.

python2 --version

Expected Output:

Python 2.7.14

The python Command

Notice that running python --version on Ubuntu 17.10 points to Python 2. This was the historical default.

Ubuntu 17.10如何配置Python环境?-图3
(图片来源网络,侵删)
python --version

Expected Output:

Python 2.7.14

This is why you should always use python3 for new projects.


Installing Python 3 (If Needed)

Ubuntu 17.10 comes with Python 3.6 pre-installed, but if for some reason it's missing, you can install it easily.

# Update your package list
sudo apt update
# Install Python 3 and its development tools
sudo apt install python3 python3-pip python3-venv
  • python3: The Python 3 interpreter.
  • python3-pip: The package installer for Python 3 (like apt but for Python libraries).
  • python3-venv: The module for creating virtual environments.

The Best Practice: Using Virtual Environments

Virtual environments are essential for Python development. They create an isolated space for each project's dependencies, preventing version conflicts.

Here’s how to set one up:

  1. Navigate to your project directory:

    cd my_awesome_project
  2. Create the virtual environment: We'll call it venv.

    python3 -m venv venv

    This command uses the venv module to create a new directory named venv containing a self-contained Python installation.

  3. Activate the virtual environment:

    source venv/bin/activate

    Your terminal prompt will change to show (venv), indicating the environment is active. Now, when you run python or pip, it will use the ones inside your venv directory.

  4. Install packages: With the environment active, install packages using pip3 (or just pip, which now points to the pip for Python 3).

    (venv) pip install requests numpy
  5. Deactivate the environment when you're done:

    (venv) deactivate

    Your prompt will return to normal.


Installing Packages with pip

Once your virtual environment is active, installing Python packages is straightforward.

# Activate your virtual environment first
source venv/bin/activate
# Install a package
pip install <package_name>
# Example: Install a web framework
pip install flask
# Upgrade a package
pip install --upgrade <package_name>
# Uninstall a package
pip uninstall <package_name>
# List installed packages
pip list

Running Python Scripts

  1. Create a simple script, hello.py:

    nano hello.py

    Add the following content:

    print("Hello from Python 3!")

    Save and exit (Ctrl+X, then Y, then Enter).

  2. Make it executable:

    chmod +x hello.py
  3. Run it using python3:

    python3 hello.py

    Output:

    Hello from Python 3!

Critical Warning: Ubuntu 17.10 is End-of-Life (EOL)

This is the most important point in this guide. Ubuntu 17.10 no longer receives security updates or bug fixes.

Running an EOL operating system on a machine connected to the internet is a significant security risk.

Strongly Recommended Action: Upgrade Your System

You should upgrade to a Long-Term Support (LTS) version like Ubuntu 20.04 LTS, 22.04 LTS, or the latest 24.04 LTS. LTS versions receive security updates for 5 years.

To upgrade, follow the official Ubuntu documentation: https://ubuntu.com/tutorials/how-to-ubuntu-2004

Summary Table for Ubuntu 17.10

Task Command Notes
Check Python 3 python3 --version Should be Python 3.6.6.
Check Python 2 python2 --version Legacy version.
Install Packages pip install <package> Only do this inside a virtual environment.
Create Venv python3 -m venv venv Creates an isolated environment for your project.
Activate Venv source venv/bin/activate Your prompt will change to (venv).
Deactivate Venv deactivate Exits the virtual environment.
Run Script python3 my_script.py Always use python3 to be explicit.
System Status End-of-Life (EOL) No security updates. Upgrade to a modern LTS version immediately.
分享:
扫描分享到社交APP
上一篇
下一篇