Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change python version in command prompt if I have 2 python version installed

Tags:

python

I have installed Anaconda 2 & 3 in my system. Anaconda 2 contains python 2.7 & Anaconda 3 contains python 3.6.

I need to run my python code using command prompt & I need to use python 3.6 While I'm running python --version, I'm getting python 2.7.14. How do I change it to python 3.6?

like image 892
Emily Johnson Avatar asked May 15 '18 11:05

Emily Johnson


People also ask

What happens if I have 2 versions of Python?

There's nothing wrong with having two versions of python installed, and it's actually quite common to do so. Usually, one would install them with different names ( python vs python3 , for example) to avoid confusion though.

Can I run 2 Python versions?

Install multiple python versions Lets suppose you want to install the releases 2.5, 3.5 and 3.9. For Windows users, I recommend using the Windows x86-64 executable installer option if you work on a 64bit system. Otherwise, just use the Windows x86 executable installer .


1 Answers

Both Python2 and Python3 is installed
As you can see, I have both Python2 and Python3 installed.

Windows Path variable
I hope you know that the path of the python executable has to be added to the PATH environment variable in Windows. As you can see, the path of Python2 is placed above the path of Python3 in my system.

How does the cmd run commands?
It searches for an executable with the same name in the directory the cmd has been opened in, then goes and searches for the command in the locations provided in the Windows PATH variable, from TOP to BOTTOM. Which means, it gets the path to Python2 before it can get to the path of Python3. Therefore, every time you type python in your cmd, it runs Python2.
Both Python2 and Python3 executables have the same name in Windows so it never runs python3.

What seems like an obvious solution?
You might think, changing the name of python.exe to python3.exe for the Python3 executable will solve your problem. You are partially right, it will work. But you have to use python3 file.py or python3 --version, which I think, is understandable. But, pip will no longer work if you change the name of the original python executable. You will no longer be able to install external packages and modules.

How can this problem be solved?
You can create an alias for the Python3 executable called python3.bat.
.exe and .bat files can be called from the cmd directly without using their extension. We always write python filename.py instead of python.exe filename.py although both are correct. The same can be done with .bat files.
Go back to the first image and notice the python3.bat file below python.exe. That is my way of calling python3 without renaming my original python executable.

python3.bat
Create a new file using notepad or something and paste this %~dp0python %*
I don't fully understand how this works except that dp0 basically runs python from inside the same directory and %* passes all the arguments to the python executable. Place this file inside your Python3 installation directory and your problem will hopefully be solved.

Python and Python3
python3 basically runs your python3.bat file, which in turn runs the python.exe from its folder and passes the arguments to it.

I hope this solves your problem.

like image 135
Diptangsu Goswami Avatar answered Nov 03 '22 00:11

Diptangsu Goswami