I have installed both versions of python that is python 2.7 and python 3.5.3. When I run python command in command prompt, python 3.5.3 interpreter shows up. How can I switch to python 2.7 interpreter?
7 Answers
Usually on all major operating systems the commands python2 and python3 run the correct version of Python respectively. If you have several versions of e.g. Python 3 installed, python32 or python35 would start Python 3.2 or Python 3.5. python usually starts the lowest version installed I think.
Hope this helps!
Comments
Solution for unix like OS.
You can use python2 and python3 to run certain version of Python
Also, you can check where are these files:
which python2
which python3
2 Comments
As has been mentioned in other answers to this and similar questions, if you're using Windows, cmd reads down the PATH variable from the top down. On my system I have Python 3.8 and 3.10 installed. I wanted my cmd to solely use 3.8, so I moved it to the top of the PATH variable and the next time I opened cmd and used python --version it returned 3.8.
Hopefully this is useful for future devs researching this specific question.
Comments
It depends on OS (and the way Python has been installed).
For most current installations:
on Windows, Python 3.x installs a
pycommand in the path that can be used that way:py -2launches Python2py -3launches Python3
On Unix-likes, the most common way is to have different names for the executables of different versions (or to have different symlinks do them). So you can normally call directly
python2.7orpython2to start that version (andpython3orpython3.5for the alternate one). By default only a part of all those symlinks can have been installed but at least one per version. Search you path to find them
Comments
However, what you can do, is create an alias for your personal use. This can be accomplished easily by adding the following line:
alias python=python3
or
alias python=/usr/bin/python3
/in the ~/.bash_aliases file - which you can edit via sudo nano ~/.bash_aliases. Then, close and reopen the terminal and you should be able to use the python command for your own personal use without it affecting the rest of the system.
However, this, again, isn't suggested because although you won't break any of the system-wide code that relies on proper placement of Python interpreters, I've heard it can cause other issues (that I don't know/remember.
python2?