Anil Soren
Anil Soren

Reputation: 63

how to switch python interpreter in cmd?

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?

Upvotes: 1

Views: 21909

Answers (7)

Michael Allen
Michael Allen

Reputation: 11

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.

Upvotes: 1

Anand Tripathi
Anand Tripathi

Reputation: 16136

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.

Upvotes: 0

Greg Eremeev
Greg Eremeev

Reputation: 1840

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

Upvotes: 1

Serge Ballesta
Serge Ballesta

Reputation: 148900

It depends on OS (and the way Python has been installed).

For most current installations:

  • on Windows, Python 3.x installs a py command in the path that can be used that way:

    • py -2 launches Python2
    • py -3 launches 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.7 or python2 to start that version (and python3 or python3.5 for 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

Upvotes: 0

linusg
linusg

Reputation: 6439

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!

Upvotes: 1

Stanislav Filin
Stanislav Filin

Reputation: 124

If you use Windows OS:

py -2.7 for python 2.7

py -3 for python 3.x

But first you need to check your PATH

Upvotes: 0

raratiru
raratiru

Reputation: 9616

In my case, /usr/bin/python is a symlink that points to /usr/bin/python2.7.

Ususally, there is a relevant symlink for python2 and python3.

So, if you type python2 you get a python-2 interpreter and if you type python3 you get a python-3 one.

Upvotes: 0

Related Questions