Table of Contents
How to Select the Default Python Version
Changing the Default Version
Using a Shell Alias
Configuring $PATH
Different Python Versions and $PATH
Managing Virtual Environments
Home Backend Development Python Tutorial How Do I Set the Default Python Version?

How Do I Set the Default Python Version?

Nov 08, 2024 pm 07:28 PM

How Do I Set the Default Python Version?

How to Select the Default Python Version

Changing the Default Version

Historically, Python versions coexisted due to compatibility concerns. To maintain backwards compatibility, newer Python versions (e.g., Python 3) did not replace Python 2 when installed. This resulted in the confusing situation where typing "python" would invoke Python 2, even after installing Python 3.

However, modern systems encourage explicitly calling specific Python versions (e.g., "python2" or "python3") in scripts and commands.

Using a Shell Alias

To conveniently execute specific Python versions, create a shell alias. Here's an example for setting the default Python version to Python 3:

alias py=python3
Copy after login

Configuring $PATH

The PATH environment variable controls which directories are searched for executable files. To prioritize specific Python versions, place the desired version's directory at the beginning of the path:

export PATH=/usr/local/bin/python3:$PATH
Copy after login

Different Python Versions and $PATH

If multiple Python versions (e.g., Python 3.1 and 3.2) are installed, the PATH variable determines which version is executed. The first occurrence of a matching program in the specified directories will be invoked.

Here's an example where "python3" resolves to Python 3.7:

echo $PATH
/usr/sbin:/usr/local/bin:/usr/sbin:usr/local/bin:/usr/bin:/bin

which python3
/usr/bin/python3

ls -l /usr/bin/python3
lrwxrwxrwx 1 root root 9 Mar 26  2019 /usr/bin/python3 -> python3.7*

ls -l /usr/bin/python3.7
-rwxr-xr-x 2 root root 4877888 Apr  2  2019 /usr/bin/python3.7*
Copy after login

Managing Virtual Environments

To isolate specific Python versions and their packages, consider using virtual environments. This allows for customized installations and independent version management.

The above is the detailed content of How Do I Set the Default Python Version?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How Do I Use Beautiful Soup to Parse HTML? How Do I Use Beautiful Soup to Parse HTML? Mar 10, 2025 pm 06:54 PM

How Do I Use Beautiful Soup to Parse HTML?

How to Use Python to Find the Zipf Distribution of a Text File How to Use Python to Find the Zipf Distribution of a Text File Mar 05, 2025 am 09:58 AM

How to Use Python to Find the Zipf Distribution of a Text File

Image Filtering in Python Image Filtering in Python Mar 03, 2025 am 09:44 AM

Image Filtering in Python

How to Perform Deep Learning with TensorFlow or PyTorch? How to Perform Deep Learning with TensorFlow or PyTorch? Mar 10, 2025 pm 06:52 PM

How to Perform Deep Learning with TensorFlow or PyTorch?

Introduction to Parallel and Concurrent Programming in Python Introduction to Parallel and Concurrent Programming in Python Mar 03, 2025 am 10:32 AM

Introduction to Parallel and Concurrent Programming in Python

Serialization and Deserialization of Python Objects: Part 1 Serialization and Deserialization of Python Objects: Part 1 Mar 08, 2025 am 09:39 AM

Serialization and Deserialization of Python Objects: Part 1

How to Implement Your Own Data Structure in Python How to Implement Your Own Data Structure in Python Mar 03, 2025 am 09:28 AM

How to Implement Your Own Data Structure in Python

Mathematical Modules in Python: Statistics Mathematical Modules in Python: Statistics Mar 09, 2025 am 11:40 AM

Mathematical Modules in Python: Statistics

See all articles