Hi @EMP_H ,
Yes, you should be able to switch between python versions. As a standard, it is recommended to use the python3 command or python3.7 to select a specific version. The py.exe launcher will automatically select the most recent version of Python you've installed. You can also use commands like py -3.7 to select a particular version, or py --list to see which versions can be used. HOWEVER, the py.exe launcher will only work if you are using a version of Python installed from python.org. When you install Python from the Microsoft Store, the py command is not included. Since you're using the Microsoft Store version of Python, you should use the python3 (or python3.7) command.
Hope that helped. Please let us know if you have further questions.
Best,
Grace
For information please read this FAQ doc for using python on windows.
Answer from Grmacjon-MSFT on learn.microsoft.comUse Virtualenv.
There is more information here: Working with virtualenv.
Using virtualenv you can create a new virtual python environment with whatever version of Python you want for each project or application. You can then activate the appropriate environment when you need it.
To expand on my answer:
You can install multiple versions of Python on your computer (I have 2.4, 2.5, 2.6 and 3.1 on my machine - I install each from source). I use a Mac, and keep my system Python as whatever OS X sets as the default.
I use easy_install to install packages. On ubuntu you can get easy_install like this:
sudo apt-get install python-setuptools
To install virtualenv then do:
easy_install virtualenv
I tend to create a new virtualenv for each project I'm working on and don't give it access to the global site-packages. This keeps all the packages tight together and allows me to have the specific versions of everything I need.
virtualenv -p python2.6 --no-site-packages ~/env/NEW_DJANGO_PROJECT
And then whenever I am doing anything related to this project I activate it:
source ~/env/NEW_DJANGO_PROJECT/bin/activate
If I run python now it uses this new python. If I use easy_install it installs things into my new virtual environment.
So, virtualenv should be able to solve all of your problems.
Pythonbrew is a magical tool. Which can also be called as Python version manager similar to that of RVM-Ruby version manager but Pythonbrew is inspired by Perlbrew.
Pythonbrew is a program to automate the building and installation of Python in the users $HOME.
Dependencies – curl
Before Installing the Pythonbrew, Install “curl” in the machine, to install curl use the below command in the terminal, give the the password for the user when prompted.
$sudo apt-get install curl
After Installing the curl, Now Install Pythonbrew, copy and paste the following commands in the terminal and type the password for the user when prompted.
Recomended method of installation - Easy Install
$ sudo easy_install pythonbrew
To complete the installation, type the following command
$pythonbrew_install
Alternate method of installation:
Use curl command to download the latest version of pythonbrew from github.
curl -kLO http://github.com/utahta/pythonbrew/raw/master/pythonbrew-install
After downloading, change “pythonbrew-install” to “executable”
chmod +x pythonbrew-install
Then, run the pythonbrew-install in the terminal
./pythonbrew-install
Now the Pythonbrew has been installed in the “Home Directory” i.e., /home/user/.pythonbrew
Next, copy and paste the following line to the end of ~/.bashrc
*NOTE: change “user” to your user name in the system
source /home/user/.pythonbrew/etc/bashrc
Thats it! Close the terminal. Steps to Install different versions of Python:
Open a new terminal, type the following command or copy and paste it.
$pythonbrew install 2.6.6
This will install Python 2.6.6 and to install Python 2.7 or Python 3.2, change the version number in the previous command.
$pythonbrew install 2.7
or
$pythonbrew install 3.2
Update: If you get error while Installing then Install using the below command.
$pythonbrew install --force 2.7
or
$pythonbrew install --force 3.2
How to manage different versions of Python installed in system
For instance, if Python 2.6.6, Python 2.7 and Python 3.2 is installed in your system, switching between the versions can be done as follows:
By default, Python 2.6.6 will be active and in order to switch to Python 2.7 use the below command
$pythonbrew switch 2.7
The default Python is changed to Python 2.7.
Now, to switch to Python 3.2 change the version number in the previous command.
$pythonbrew switch 3.2
Use the below command to check or list the installed Python versions
$pythonbrew list
Use the below command to check or list the available Python Versions to install
$pythonbrew list -k
To uninstall any of the installed Python version (for example to uninstall Python 2.7), use the below command.
$pythonbrew uninstall 2.7
Use the below command to update the Pythonbrew
$pythonbrew update
Use the below command to disable the Pythonbrew and to activate the default version
$pythonbrew off
Enjoy the experience of installing multiple versions of Python in single Linux / ubuntu machine!
How can I switch default python version on command line?
How to downgrade python from 3.7 to 3.6 - Stack Overflow
How to install an older version of python - Stack Overflow
What is the best way to upgrade an old version of python
Videos
The simplest way would be to add an alias to python3 to always point to the native python installed. Add this line to the .bash_profile file in your $HOME directory at the last,
alias python="python3"
Doing so makes the changes to be reflected on every interactive shell opened.
pyenv is a 3rd party version manager which is super commonly used (18k stars, 1.6k forks) and exactly what I looked for when I came to this question.
edit: I use it for several years now. Works like a charm.
Installation
Install pyenv.
Usage
$ pyenv install --list
Available versions:
2.1.3
[...]
3.8.1
3.9-dev
activepython-2.7.14
activepython-3.5.4
activepython-3.6.0
anaconda-1.4.0
[... a lot more; including anaconda, miniconda, activepython, ironpython, pypy, stackless, ....]
$ pyenv install 3.8.1
Downloading Python-3.8.1.tar.xz...
-> https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tar.xz
Installing Python-3.8.1...
Installed Python-3.8.1 to /home/moose/.pyenv/versions/3.8.1
$ pyenv versions
* system (set by /home/moose/.pyenv/version)
2.7.16
3.5.7
3.6.9
3.7.4
3.8-dev
$ python --version
Python 2.7.17
$ pip --version
pip 19.3.1 from /home/moose/.local/lib/python3.6/site-packages/pip (python 3.6)
# Writing the .python-version file does the switch:
$ mkdir pyenv-experiment && echo "3.8.1" > "pyenv-experiment/.python-version"
# alternatively, you could use `pyenv local 3.8.1`
$ cd pyenv-experiment
$ python --version
Python 3.8.1
$ pip --version
pip 19.2.3 from /home/moose/.pyenv/versions/3.8.1/lib/python3.8/site-packages/pip (python 3.8)
I have macOS and downloaded VSCode which I think comes with Python 3.12. Then, I downloaded Python 3.8 from the official website. Now, Python 3.8 is the default version. How can I switch default to Python 3.12 without deleting 3.8? Thanks in advance!
Here is a canonical summary which sums up different solutions for the variety of operating system Python runs on. What follows are possibilities for Microsoft Windows, Linux, macOS and Misc.
As mentioned those are just possibilities - by no means do I claim to have a complete list whatsoever.
Microsoft Windows
Option 1
In general, it's suggested to use virtual environments (I highly suggest looking at the official Python documentation). With this approach, you easily can set up project-specific Python versions (as well as libraries). Easily manageable and the best part: There are lots of tutorials on the internet on how to approach this:
- Using VirtualEnv with multiple Python versions on windows
- https://www.freecodecamp.org/news/installing-multiple-python-versions-on-windows-using-virtualenv/
- etc.
1.) Open command prompt ("cmd") and enter pip install virtualenv.
2.) Install your desired Python version via https://www.python.org/downloads ; Remember: Do not add to PATH!
3.) Type into the command prompt: virtualenv \path\to\env -p \path\to\python_install.exe, whereas \path\to\env shall be the path where your virtual environment is going to be and \path\to\python_install.exe the one where your freshly (presumably) installed Python version resides.
4.) Done! You now have a virtual environment set up! Now, to activate the virtual environment execute the batch file which is located inside the \path\to\env\Scripts\activate.bat. (cf. this website or an official Python guide)
Option 2
The basic option would be to uninstall the unwanted Python version and re-install the favored one from https://www.python.org/downloads/. To remove the "old" version go to Control Panel -> "Uninstall a program" -> Search for "Python" -> Right-click on the Python name -> Uninstall. Bear in mind that Python usually has a PATH variable stored, hence you should remove it as well - Check the following links for this:
- https://support.foundry.com/hc/en-us/articles/209642805-Q100127-How-to-delete-unset-environment-variables
- Remove unwanted path name from %path% variable via batch
Now double-check whether there are any remaining files where Python used to be stored. Usually, you can find all the Python files at either C:\Program Files (x86)\Pythonxx, C:\Users\username\AppData\Local\Programs\Pythonxx or C:\Pythonxx or all of them. You might have installed it in another directory - check where it once was.
Now after de-installing just re-install the wanted version by going to the download page and follow the usual installation process. I won't go into details on how to install Python.. Lastly, you might check which version is currently installed by opening the command prompt and typing python -V.
Option 3
This approach is pretty similar to the second one - you basically uninstall the old one and replace it by your favored version. The only thing that changes it the part regarding how to uninstall the unwanted Python distribution: Simply execute the Python3 installer you originally used to install Python (it's usually stored in your Python directory as mentioned above; for more assistance check out this). There you get an option to repair or uninstall, proceed by choosing uninstall, and follow the steps provided via the uninstaller.
No matter how you uninstall Python (there are many resources on this topic, for example this Stack Overflow question or a problem thread a user by the name of Vincent Tang posted on the Stack Exchange site Super User, etc.), just reinstall the wanted Python version by following the steps mentioned in Option 2.
Option 4
Option 4 deals with Anaconda. Please refer to this site on how to install Anaconda on Windows. Step 9 is important as you don't want to install it as your default Python - you want to run multiple versions of Python:
Choose whether to register Anaconda as your default Python. Unless you plan on installing and running multiple versions of Anaconda or multiple versions of Python, accept the default and leave this box checked.
Follow the official tutorial I linked above.
Once done you can create the following commands individually in the anaconda prompt: To overwrite the default python version system-wise use conda install python=3.6 or to create a virtual environment go ahead and use conda create -n $PYTHON36_ENV_NAME python=3.6 anaconda whereas $PYTHON36_ENV_NAME is the custom name you can set. Credit where credit is due - the user @CermakM from this thread strongly influenced this snippet.
In my research I encountered a bunch of useful Stack Overflow threads - you might check them out if you go the tough road with Anaconda:
- How to downgrade the Python Version from 3.8 to 3.7 on windows?
- downgrade python version from 3.8 to lower one in a given conda environment
Option 5
What follows isn't a downgrade in the classical sense - though for the sake of completeness I decided to mention this approach as well. On Windows you're also able to run multiple Python versions - an infamous thread on StackOverflow deals with this question, thus I politely refer you to there for further reading purposes.
Linux
Option 1
Pretty analog to the third option for Windows I highly suggest you use a virtual environment such as Anaconda. Anaconda - or short conda - is also available on Linux. Check the official installation documentation here. Once again this thread is highly suggested on how to overwrite a Python version, respectively how to specifically create an environment with your wanted Python version.
Option 2
Another highly suggested virtual environment is Pyenv. The user @Sawan Vaidya described in this Stack Overflow question on how to up-or downgrade a Python version with the help of Pyenv. You can either set a Python version globally or create a local environment - both explained in the mentioned thread.
Option 3
Another user, namely @Jeereddy, has suggested to use the software package management system Homebrew. He explained this option thoroughly in this current question:
$ brew unlink python
$ brew install --ignore-dependencies https://raw.githubusercontent.com/Homebrew/homebrew-core/e128fa1bce3377de32cbf11bd8e46f7334dfd7a6/Formula/python.rb
$ brew switch python 3.6.5
Option 5
No need to reinvent the wheel - this thread is filled with lots of beautiful running approaches such as the one by @Sidharth Taneja.
- Download your wanted Python version from https://www.python.org/downloads/release and install it as a
normal package. - Run
cd /Library/Frameworks/Python.framework/Version - Execute
lsto list all installed Python versions - Run
sudo rm -rf 3.7, removing Python version 3.7 - can be repeated for whatever version(s) you want to delete - Check
python3 -v, it should display the version you originally wanted to have installed
Option 6
What a goldmine this thread is! As @nondetermistic has described in-depth (direct link to his post):
Install Python source code as it is like this:
#Taken Python 3.6 as an example
$ mkdir /home/<user>/python3.6
$ ./configure --prefix=/home/<user>/python3.6/
$ make altinstall
You're now able to either add the downloaded version (/home/<user>/python3.6/bin) to PATH as well as lib to LD_LIBRARY_PATH or just create a virtual environment by: /home/<user>/python3.6/bin/python3.6 -m venv env-python3.6. A very aesthetic and simple solution to run multiple Python versions on your system.
macOS
Option 1
Using pyenv with Homebrew - credit to @Shayan with his reply here:
1.) Installing pyenv with Homebrew:
brew update
brew install pyenv
2.) Cloning the GitHub repository to get latest pyenv version:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
3.) Defining the environment variables as follows
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
4.) Restarting shell so PATH change works
exec "$SHELL"
5.) Checking the available Python versions
pyenv install --list
6.) Installing the wanted Python version, e.g. 3.6
pyenv install 3.6
7.) Setting it globally (you can also go ahead and only use it in a certain environment)
pyenv global 3.6
8.) Check the Python version the system is using - your wanted / downgraded version should be displayed here.
python3 --version
Option 2
Similar to previous approaches you can download Anaconda on macOS as well. For an installation guide click here. The usage is pretty much the same as I've already described in Option 4 of the Windows guide. Please check out above.
Other options
In this case it's getting pretty repetitive. I kindly suggest you to check the following resources for further reading:
- https://prodevsblog.com/questions/125949/macos-how-to-downgrade-homebrew-python/
- https://www.xspdf.com/resolution/51291818.html
- How to downgrade python version from 3.8 to 3.7 (mac)
- https://weknowinc.com/blog/running-multiple-python-versions-mac-osx
Misc
When writing this post I had the problem of not knowing where to draw the line. When looking up the operating systems Python currently supports you get a huge list, including the ones I mentioned, i.e. Linux, Microsoft Windows and macOS, though obviously different Linux distributions are single-handedly treated, e.g. CentOS, Arch Linux or Fedora should deserve a spot as well - or shall I make separate options for Windows 10, 7, etc.?
Due to the high degree of repetitiveness as far as modules like Homebrew, Conda or alike are concerned I decided to limit my list to the "main three" operating systems - distributions like Ubuntu (How do I downgrade my version of python from 3.7.5 to 3.6.5 on ubuntu), CentOS (How to downgrade python version on CentOS?) can be easily researched on Stack Overflow. Most often than not you can apply solutions from the Linux tab for said distributions. The same goes with Windows and macOS (versions).
If you are working with Anaconda, then
conda install python=3.5.0
# or maybe
conda install python=2.7.8
# or whatever you want....
might work.
All the answers here are outdated since Python.org doesn't host installers for older versions of Python anymore, only source code.
And building Python on Windows is not really a walk in the park...
My solution: Pyenv.
It's crossplatform (Linux, MacOS, Windows: where it's called pyenv-win), and you can with it automatically install among a very large list of Python versions.
There aren't ALL python versions existing but the list is already very big.
Installation pf pyenv is quite easy if you use chocolatey.
Then:
pyenv install --list : all the versions that you can install.
and then:
pyenv install 3.9.0 for example.
Python.org DOES host installers for older versions
For example if you want to download version 3.4 you just have to find major release which is:
https://www.python.org/ftp/python/3.4.4/
In case release is only bugfix you want be able to find installer so than just change last digit in url until you find release with installer, so
https://www.python.org/ftp/python/3.4.**x**/
change x until you find installer for your release.
Or, browse the parent folder and find the version you want:
https://www.python.org/ftp/python/
Optionally- than you can find latest bugfix and try to install it manually.
I encourage to install just your python dependencies using a python package, as this other question and answer suggest: How do I install a different Python version using apt-get?
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.9
Install anaconda https://phoenixnap.com/kb/how-to-install-anaconda-ubuntu-18-04-or-20-04
Anaconda manages different versions of python and their packages by creating environments that are isolated.
So you can install whatever versions of python without crushing.
conda create --name envp39 python=3.9
This creates python environment with python 3.9
You can switch version by changing environments with
conda activate envp39
You have to open/install the software in a shell with activated envp39.