Python

How to change default Python version

19 September 2026 · 15 min read

How to change default Python version

Python, known for its versatility and ease of use, is a staple in various fields, from web development to data science. However, managing different Python versions on a single system can quickly become a headache. Whether you’re juggling legacy projects requiring older Python versions or experimenting with the newest features in the latest release, knowing how to change default Python version is crucial. This guide provides a comprehensive walkthrough of several methods to seamlessly switch between Python installations, ensuring a smooth development experience. We will delve into practical strategies, including using the update-alternatives command on Linux systems, utilizing the py launcher on Windows, and leveraging virtual environments for project-specific configurations. Mastering these techniques will empower you to manage multiple Python versions efficiently, resolving dependency conflicts and optimizing your workflow.

Understanding Python Version Management

Before diving into the methods for switching Python versions, it’s essential to understand why this is necessary. Many operating systems come with a pre-installed version of Python, often Python 2. However, Python 2 reached its end-of-life in 2020, and Python 3 is now the standard. Furthermore, different projects may require specific Python versions due to library compatibility or framework dependencies. Attempting to run a project with an incompatible Python version can lead to errors and unexpected behavior. For instance, a data science project using libraries like TensorFlow or PyTorch might need a specific Python 3 version to function correctly. According to a Stack Overflow Developer Survey, a significant portion of developers still encounter version compatibility issues, highlighting the importance of effective Python version management. Properly managing your Python installations is essential for a smooth and productive development workflow.

Effective Python version management involves not only installing multiple versions but also having a reliable way to switch between them as needed. This process allows you to isolate project dependencies, preventing conflicts and ensuring that each project runs in its intended environment. Tools like virtual environments, pyenv, and system-level configurations offer different approaches to address this challenge. Understanding the nuances of each method will help you choose the best strategy for your specific needs. Choosing the right method depends on factors like your operating system, the number of Python versions you need to manage, and the complexity of your projects.

Consider a scenario where you’re working on two projects: one built on Django 1.11 (compatible with Python 2.7 or Python 3.4+) and another using the latest Django version (requiring Python 3.7+). Without proper version management, you would face conflicts and potential incompatibility issues. Having the ability to switch between Python versions allows you to run each project in its appropriate environment, ensuring smooth development and deployment. For example, using virtual environments, you can create isolated Python installations for each project, complete with their specific dependencies. This prevents library conflicts and ensures that each project runs with the correct Python version.

Methods to Change Default Python Version on Linux

On Linux systems, the update-alternatives command is a powerful tool for managing default versions of various software, including Python. This command creates symbolic links that point to the desired version, allowing you to switch between different installations easily. To use update-alternatives, you first need to ensure that you have multiple Python versions installed on your system. You can typically install additional versions using your distribution’s package manager, such as apt on Debian/Ubuntu or yum on CentOS/RHEL. Once you have multiple versions installed, you can then configure them using update-alternatives. This method is particularly useful for setting system-wide defaults, affecting all users on the system. According to the Debian documentation, update-alternatives is the recommended way to manage default commands on Debian-based systems.

Here’s how to use update-alternatives to change default Python version:

  1. First, check which Python versions are installed on your system by running python --version or python3 --version.
  2. Next, use the following commands to register each Python version with update-alternatives: ``` sudo update-alternatives –install /usr/bin/python python /usr/bin/python2.7 1 sudo update-alternatives –install /usr/bin/python python /usr/bin/python3.6 2
    
    Replace `/usr/bin/python2.7` and `/usr/bin/python3.6` with the actual paths to your Python executables. The numbers `1` and `2` represent the priority of each version; higher numbers indicate higher priority.
    
  3. Finally, use the following command to select the default Python version: ``` sudo update-alternatives –config python
    
    This will present you with a list of available Python versions, and you can choose the one you want to set as the default.
    

Alternatively, you can directly set the default version by specifying the selection number:

sudo update-alternatives --set python /usr/bin/python3.6

After running these commands, verify the change by running python --version to confirm that the default Python version has been updated. Keep in mind that this method changes the system-wide default, so it will affect all scripts and applications that rely on the python command. It’s crucial to test your applications after making this change to ensure they continue to function correctly. Consider using virtual environments for project-specific Python versions to avoid potential conflicts. Managing Python Versions on Windows with py Launcher

Windows uses the Python Launcher (py.exe) to manage different Python versions. When you install multiple Python versions on Windows, the launcher automatically associates each version with a specific identifier. This allows you to specify which Python version to use when running scripts from the command line. The py launcher searches for Python installations in the Windows registry and uses these entries to determine which versions are available. It also allows you to specify a default version using the PY_PYTHON environment variable or by creating a py.ini file. This makes it easier to manage different Python versions without modifying system-wide settings directly.

To change default Python version using the py launcher, you can use the following steps:

  • First, ensure that you have multiple Python versions installed and that they are properly registered in the Windows registry. During installation, make sure to select the option to add Python to the PATH environment variable for each version.
  • Next, you can specify the Python version to use by adding a shebang line at the beginning of your Python script (e.g., ! python3 or ! python2).
  • Alternatively, you can use the -3 or -2 flags with the py command to specify the Python version (e.g., py -3 script.py to run the script with Python 3).

For example, to set Python 3.9 as the default, you can create a py.ini file in your user directory (%USERPROFILE%) or in the same directory as your script with the following content:

[defaults] python = 3.9

Virtual environments are a crucial tool for isolating Python projects and managing their dependencies. A virtual environment creates a self-contained directory that contains a specific Python version and its associated packages. This allows you to install packages without affecting the system-wide Python installation or other projects. Virtual environments are particularly useful when working on multiple projects with conflicting dependencies. For example, one project might require an older version of a library, while another project needs the latest version. By using virtual environments, you can avoid these conflicts and ensure that each project has its own isolated environment. The venv module, introduced in Python 3.3, provides a standard way to create virtual environments.

To create and activate a virtual environment, follow these steps:

  1. Open your terminal or command prompt and navigate to your project directory.
  2. Create a virtual environment using the following command: ``` python3 -m venv .venv
    
    This will create a directory named `.venv` (you can choose any name) containing the virtual environment files.
    
  3. Activate the virtual environment using the appropriate command for your operating system:
    • On Linux/macOS: ``` source .venv/bin/activate
    • On Windows: ``` .venv\Scripts\activate
  4. Once the virtual environment is activated, your terminal prompt will be prefixed with the environment name (e.g., (.venv)).
  5. You can now install packages using pip, and they will be installed only within the virtual environment.
  6. To deactivate the virtual environment, simply run the deactivate command.

Using virtual environments ensures that each project has its own isolated environment, preventing dependency conflicts and ensuring that each project runs with the correct Python version and dependencies. For example, if you need to use Python 3.7 for one project and Python 3.9 for another, you can create separate virtual environments for each project, each with its specific Python version and dependencies. This approach also makes it easier to manage project dependencies and deploy your applications, as you can simply copy the virtual environment to the deployment environment. According to the Python Packaging Authority (PyPA), virtual environments are the recommended way to manage dependencies in Python projects. By using virtual environments, you can ensure that your projects are isolated, reproducible, and easy to manage.

Featured snippet optimized paragraph: Are you struggling with conflicting Python dependencies across different projects? Using virtual environments is the ideal solution. Virtual environments create isolated spaces for each project, allowing you to specify the Python version and install dependencies without affecting other projects or the system-wide Python installation. This ensures consistency and avoids conflicts, making your development process smoother and more reliable. You can create a virtual environment using the venv module in Python, and activate it to start working within the isolated environment. This practice is highly recommended for managing Python projects effectively.

Infographic here
FAQ: Changing the Default Python Version ----------------------------------------
Why would I need to change the default Python version?
Different projects may require different Python versions due to compatibility issues with libraries or frameworks. Changing the default version allows you to run projects with the correct Python environment.
What is the best way to manage Python versions on Linux?
The `update-alternatives` command is a powerful tool for managing default versions of software, including Python, on Linux systems. It allows you to easily switch between different Python installations.
How can I specify a Python version when running a script on Windows?
You can use the Python Launcher (`py.exe`) and specify the version using a shebang line in the script (e.g., `! python3`) or by using the `-3` or `-2` flags with the `py` command.
What are virtual environments and why should I use them?
Virtual environments create isolated directories that contain a specific Python version and its associated packages. They prevent dependency conflicts and ensure that each project has its own isolated environment.
How do I create a virtual environment?
You can create a virtual environment using the `venv` module in Python. Open your terminal or command prompt, navigate to your project directory, and run `python3 -m venv .venv`.
Mastering the art of managing your Python versions opens up a world of possibilities, allowing you to tackle diverse projects with ease and confidence. We've explored different methods, from using `update-alternatives` on Linux to leveraging the py launcher on Windows and the power of virtual environments for project isolation. By implementing these strategies, you can avoid common dependency conflicts and ensure that your projects run smoothly in their intended environments.

Ready to take your Python skills to the next level? Experiment with the methods we’ve discussed, and don’t hesitate to explore further resources and documentation for a deeper understanding. Consider reading more about [advanced virtual Question & Answer :
I have installed Python 3.2 on my Mac. After I run /Applications/Python 3.2/Update Shell Profile.command, it’s confusing that when I type python -V in Terminal, it says Python 2.6.1 which is not what I expected.

How can I change the default Python version?

[updated for 2021]

(Regardless if you are on Mac, Linux, or Windows:)

If you are confused about how to start the latest version of python, on most platforms it is the case that python3 leaves your python2 installation intact (due to the above compatibility reasons); thus you can start python3 with the python3 command.

Historically…

The naming convention is that generally, most scripts will call python2 or python3 explicitly. This happened due to a need for backwards compatibility.

Even though technically python doesn’t even guarantee backwards compatibility between minor versions, Python3 really breaks backwards compatibility. At the time, programs invoking ‘python’ were expecting python2 (which was the main version at the time). Extremely old systems may have programs and scripts which expect python=python2, and changing this would break those programs and scripts.

At the time this answer was written, OP should not have changed this due to maintaining compatibility for old scripts.

Circa year 2021…

Nowadays, many years after the python2->python3 transition, most software explicitly refers to python2 or python3 (at least on Linux). For example, they might call #!/usr/bin/env python2 or #!/usr/bin/env python3. This has for example (python-is-python3-package) freed up the python command to be settable to a user default, but it really depends on the operating system.

The prescription for how distributions should handle the python command was written up in 2011 as PEP 394 – The “python” Command on Unix-Like Systems. It was last updated in June 2019.

Regardless of whether you are writing a library or your program, you should specify the version of python (2 or 3, or finer-grained under specific circumstances) you can use in the shebang line, or since you’re on OS X, in your IDE with which you are developing your app, so it doesn’t mess up the rest of the system (this is what python venvs are for… download and search how to up set up a python3 venv on Mac if you’re on a really really old version of OS X).

Shell alias: -———–

You could, however, make a custom alias in your shell. The way you do so depends on the shell, but perhaps you could do alias py=python3, and put it in your shell startup file (.bashrc, .zshrc, etc). This will only work on your local computer (as it should), and is somewhat unnecessary compared to just typing it out (unless you invoke the command constantly).

Confused users should not try to create aliases or virtual environments or similar that make python execute python3; this is poor form.This is acceptable nowadays, but PEP 394 suggests encouraging users to use a virtualenv instead.

Different 3.* versions, or 2.* versions: -—————————————–

In the extremely unlikely case that if someone comes to this question with two python3 versions e.g. 3.1 vs 3.2, and you are confused that you have somehow installed two versions of python, this is possibly because you have done manual and/or manual installations. You can use your OS’s standard package/program install/uninstall/management facilities to help track things down, and perhaps (unless you are doing dev work that surprisingly is impacted by the few backwards-incompatible changes between minor versions) delete the old version (or do make uninstall if you did a manual installation). If you require two versions, then reconfigure your $PATH variable so the ‘default’ version you want is in front; or if you are using most Linux distros, the command you are looking for is sudo update-alternatives. Make sure any programs you run which need access to the older versions may be properly invoked by their calling environment or shell (by setting up the var PATH in that environment).

A bit about $PATH -—————-

sidenote: To elaborate a bit on PATH: the usual ways that programs are selected is via the PATH (echo $PATH on Linux and Mac) environment variable. You can always run a program with the full path e.g. /usr/bin/🔳 some args, or cd /usr/bin then ./🔳 some args (replace blank with the ’echo’ program I mentioned above for example), but otherwise typing 🔳 some args has no meaning without PATH env variable which declares the directories we implicitly may search-then-execute files from (if /usr/bin was not in PATH, then it would say 🔳: command not found). The first matching command in the first directory is the one which is executed (the which command on Linux and Mac will tell you which sub-path this is). Usually it is (e.g. on Linux, but similar on Mac) something like /usr/bin/python which is a symlink to other symlinks to the final version somewhere, e.g.:

% echo $PATH /usr/sbin:/usr/local/bin:/usr/sbin:usr/local/bin:/usr/bin:/bin % which python /usr/bin/python % which python2 /usr/bin/python2 % ls -l /usr/bin/python lrwxrwxrwx 1 root root 7 Mar 4 2019 /usr/bin/python -> python2* % ls -l /usr/bin/python2 lrwxrwxrwx 1 root root 9 Mar 4 2019 /usr/bin/python2 -> python2.7* % ls -l /usr/bin/python2.7 -rwxr-xr-x 1 root root 3689352 Oct 10 2019 /usr/bin/python2.7* % 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* % ls -l /usr/bin/python* lrwxrwxrwx 1 root root 7 Mar 4 2019 /usr/bin/python -> python2* lrwxrwxrwx 1 root root 9 Mar 4 2019 /usr/bin/python2 -> python2.7* -rwxr-xr-x 1 root root 3689352 Oct 10 2019 /usr/bin/python2.7* lrwxrwxrwx 1 root root 9 Mar 26 2019 /usr/bin/python3 -> python3.7* -rwxr-xr-x 2 root root 4877888 Apr 2 2019 /usr/bin/python3.7* lrwxrwxrwx 1 root root 33 Apr 2 2019 /usr/bin/python3.7-config -> x86_64-linux-gnu-python3.7-config* -rwxr-xr-x 2 root root 4877888 Apr 2 2019 /usr/bin/python3.7m* lrwxrwxrwx 1 root root 34 Apr 2 2019 /usr/bin/python3.7m-config -> x86_64-linux-gnu-python3.7m-config* lrwxrwxrwx 1 root root 16 Mar 26 2019 /usr/bin/python3-config -> python3.7-config* lrwxrwxrwx 1 root root 10 Mar 26 2019 /usr/bin/python3m -> python3.7m* lrwxrwxrwx 1 root root 17 Mar 26 2019 /usr/bin/python3m-config -> python3.7m-config* 

sidenote2: (In the rarer case a python program invokes a sub-program with the subprocess module, to specify which program to run, one can modify the paths of subprocesses with sys.path from the sys module or the PYTHONPATH environment variable set on the parent, or specifying the full path… but since the path is inherited by child processes this is not remotely likely an issue.)](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c)