Easily Switch Python Versions on Windows, macOS, and Linux

1. Introduction

Python is frequently updated, offering a wide range of options from older releases to the latest versions. However, many projects depend on specific Python versions, so proper version management and switching are necessary. This article explains the basic knowledge and practical methods useful for switching Python versions. It aims to be easy for beginners to understand, so let’s start by reviewing the basics.

2. How to Check Your Python Version

How to Check the Version from the Command Line

On Windows, macOS, and Linux you can use the command line or terminal to check the Python version.
  • Command:
  python --version
or
  python3 --version
The command will display the Python version currently in use (e.g., Python 3.10.4).

How to Check the Version Inside a Python Script

You may also want to display the current version from within Python code. Running code like the following will retrieve the version information.
import sys
print(sys.version)
This lets you check not only the version but also specific build numbers and environment details, which is useful for precise version management.

3. How to Switch Versions

The method for switching Python versions depends on your OS and the tools you use. Below are methods for Windows, macOS, and Linux.

On Windows

On Windows, you can easily switch Python versions by using the Python launcher (py.exe) or by changing your environment variable settings.
  • Switching using the Python launcher
  • By using the Python launcher (py.exe) that comes with Windows, you can easily switch between installed Python versions.
    • Example command:
    py -2 # Use Python 2.x py -3 # Use Python 3.x
  • Switching using environment variables
  • If you want to change the default Python version, update the Python executable path in Windows’ Environment Variables settings. This makes the python command point to the version you specify.

On macOS and Linux

On macOS and Linux, you can use a tool called pyenv to easily install, manage, and switch between multiple Python versions.
  • pyenv installation steps
  • To install pyenv, use the following command. curl https://pyenv.run | bash
  • This command completes the installation of pyenv. After installation, you can use the pyenv install command to install a specific Python version.
  • How to switch versions
  • To switch Python versions using pyenv, use the following command. pyenv global 3.10.4 # Set the global Python version pyenv local 3.9.1 # Set the version locally (for a specific project)

4. Using Virtual Environments

If you need to manage different Python versions or libraries for each project, creating a virtual environment is useful. By using a virtual environment, you can isolate the Python version and dependencies for a specific project.

How to Use venv and virtualenv

  • How to use venv
  • Since Python 3.3, venv is included by default, allowing you to easily create virtual environments. python -m venv myenv source myenv/bin/activate # Activate the virtual environment
  • How to use virtualenv
  • virtualenv is a tool for creating virtual environments that can also be used with Python 2.x, and is convenient when you want to use a specific Python version. virtualenv -p /usr/bin/python3.8 myenv
Using virtual environments makes it easy to specify different Python versions per project and maintain library compatibility.

5. Precautions When Switching Versions

When switching Python versions, there are a few important considerations. Below are the key points.

Checking Library Compatibility and Dependencies

Libraries may be incompatible across different Python versions, so extra care is required when migrating from Python 2 to 3. Also, the versions of libraries available vary by Python release, so manage your versions while verifying dependencies as needed.

Impact of Changing the System-Wide Python Version

Changing the system-wide Python version can affect other applications and system scripts. In particular, many parts of a Linux system rely on Python, and changing the version can cause unexpected behavior.

6. Summary

Managing Python versions is an essential skill that directly affects project stability and development efficiency. By applying the version-checking methods, OS-specific switching techniques, and virtual environment strategies introduced in this article, you can build the optimal Python environment.
侍エンジニア塾