How to Change Python Version in Command Prompt If I Have 2 Python Version Installed

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

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.

How to run different python versions in cmd

Python 3.3 introduces Python Launcher for Windows that is installed into c:\Windows\ as py.exe and pyw.exe by the installer. The installer also creates associations with .py and .pyw. Then add #!python3 or #!python2 as the first lline. No need to add anything to the PATH environment variable.

Update: Just install Python 3.3 from the official python.org/download. It will add also the launcher. Then add the first line to your script that has the .py extension. Then you can launch the script by simply typing the scriptname.py on the cmd line, od more explicitly by py scriptname.py, and also by double clicking on the scipt icon.

The py.exe looks for C:\PythonXX\python.exe where XX is related to the installed versions of Python at the computer. Say, you have Python 2.7.6 installed into C:\Python27, and Python 3.3.3 installed into C:\Python33. The first line in the script will be used by the Python launcher to choose one of the installed versions. The default (i.e. without telling the version explicitly) is to use the highest version of Python 2 that is available on the computer.

How to ensure I use the correct python version when I have multiple versions of Python installed?

Having multiple versions on the same machine is perfectly "okay", as long as you understand how to select/use the correct python version, which is the more pertinent and useful question you should be asking yourself.

They would normally be installed in separate locations and would have separate site-packages (from How do I find the location of my Python site-packages directory?):

~$ python3.7 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages']

~$ python3.8 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages']

~$ python3.9 -c 'import site; print(site.getsitepackages())'
['/usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages']

The recommended way however is to not install to these site-packages folder directly and use a virtual environment instead. There are many variations of virtual env packages/tools, but you can start with the Python docs on Virtual Environments if you are not familiar or using one yet.

With virtual environments, you can usually indicate which version of python to use when creating the virtual env, so that whenever you activate that same env, it would use the same version you used to create it.

tmp$ python3.7 -m venv app1
tmp$ source ./app1/bin/activate
(app1) tmp$ python -V
Python 3.7.12
(app1) tmp$
(app1) tmp$ deactivate

tmp$ python3.8 -m venv app2
tmp$ source ./app2/bin/activate
(app2) tmp$ python -V
Python 3.8.12
(app2) tmp$
(app2) tmp$ deactivate

tmp$ python3.9 -m venv app3
tmp$ source ./app3/bin/activate
(app3) tmp$ python -V
Python 3.9.9
(app3) tmp$
(app3) tmp$ deactivate

In the above example, I created 3 virtual environments for 3 hypothetical apps, each using a different Python version. As long as I activate the correct virtual environment, I don't have to think about which version python refers to, as it will use the same version used to create the env. See also How do I check what version of Python is running my script?.

As for installing packages, again, once you have setup the correct virtual environments, doing pip install would ensure it installs only on the site-packages on that environment, and the app running on that env would be able to import that package.

If not using a virtual environment, as I noted in my comment, the answers at Dealing with multiple Python versions and PIP? provide good suggestions on how to ensure you are installing packages for the correct Python version, namely with

$ </path/or/alias/to/specific/python/installation> -m pip install <packages>
$ python3.7 -m pip install "flake8<=3.6"
$ python3.7 -m pip list | grep flake8
flake8 3.6.0
$ ls -H /usr/local/Cellar/python@3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages | grep flake8
flake8
flake8-3.6.0.dist-info
flake8_quotes
flake8_quotes-3.2.0.dist-info

$ python3.9 -m pip install flake8
$ python3.9 -m pip list | grep flake8
flake8 4.0.1
$ ls -H /usr/local/Cellar/python@3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages | grep flake8
flake8
flake8-4.0.1.dist-info

The above example shows 2 different versions of flake8 installed for 2 different versions of Python, each on their own site-packages folder.

But when I run my command prompt and check my python version it says I am using the recent version. The 3.10.0.

This means the system default for python or python3 on your machine points to the 3.10 installation. Mine is set to point to Python 3.9:

tmp$ python3 -V
Python 3.9.9
tmp$ which python3
/usr/local/bin/python3
tmp$ /usr/local/bin/python3 -V
Python 3.9.9

You can:

  1. Manually change the default version
  2. Setup aliases to the different versions:
    tmp$ type python3.7
    python3.7 is aliased to `/usr/local/opt/python@3.7/bin/python3'

    tmp$ type python3.8
    python3.8 is aliased to `/usr/local/opt/python@3.8/bin/python3'

    tmp$ type python3.9
    python3.9 is aliased to `/usr/local/opt/python@3.9/bin/python3'
  3. Use a version management tool for switching versions, such as pyenv

Windows Command Line Python Change Version

It depends on your operating system. If you have python 2.6 installed, you need to change your environment path to point to the 2.6 executable rather than the 2.5 executable. Do a Google search for changing your PATH variable on your operating system.

How to run multiple Python versions on Windows

Running a different copy of Python is as easy as starting the correct executable. You mention that you've started a python instance, from the command line, by simply typing python.

What this does under Windows, is to trawl the %PATH% environment variable, checking for an executable, either batch file (.bat), command file (.cmd) or some other executable to run (this is controlled by the PATHEXT environment variable), that matches the name given. When it finds the correct file to run the file is being run.

Now, if you've installed two python versions 2.5 and 2.6, the path will have both of their directories in it, something like PATH=c:\python\2.5;c:\python\2.6 but Windows will stop examining the path when it finds a match.

What you really need to do is to explicitly call one or both of the applications, such as c:\python\2.5\python.exe or c:\python\2.6\python.exe.

The other alternative is to create a shortcut to the respective python.exe calling one of them python25 and the other python26; you can then simply run python25 on your command line.

Two versions of python in one computer

The preferred way to use python virtual environments now is venv.

You can install any number of python versions on your windows, though please also install the py launcher that it comes with. This makes it easy to launch whichever python version you want through the commandline, with no need to rely on PATH nonsense.

If you've the py launcher you can simply launch your desired python version using-

py -3.6-64

The above will launch the 64 bit version of python 3.6 (if installed).

Now, you'll also want to use a virtual env and point pycharm to the venv. To make a venv, go to your project directory (preferably) and do-

> py -3.6-64 -m venv name_of_venv

This will make a venv named of name_of_venv in your project directory and the python version will be 3.6 64 bit.

Now whenever you need to do any python commands, you no longer need to do py -version, you can just do python and even use pip - BUT before that, you need to activate the venv

> & '.\name_of_venv\Scripts\Activate.ps1'

Now you can do normal python operations as long within this venv and it'll all target 3.6 64 bit (or any other version you choose to build the venv with).

To deactivate (though you don't really have to) - you can just type deactivate in the terminal.

Pycharm can be configured with this venv as simply as just pointing to it. You simply have to go to Add python interpreter and choose Virtualenv Environment



Related Topics



Leave a reply



Submit