Multiple Python Versions on the Same MAChine

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.

Multiple Python versions on the same machine?

I think it is totally independent. Just install them, then you have the commands e.g. /usr/bin/python2.5 and /usr/bin/python2.6. Link /usr/bin/python to the one you want to use as default.

All the libraries are in separate folders (named after the version) anyway.

If you want to compile the versions manually, this is from the readme file of the Python source code:

Installing multiple versions

On Unix and Mac systems if you intend to install multiple versions of Python
using the same installation prefix (--prefix argument to the configure
script) you must take care that your primary python executable is not
overwritten by the installation of a different version. All files and
directories installed using "make altinstall" contain the major and minor
version and can thus live side-by-side. "make install" also creates
${prefix}/bin/python3 which refers to ${prefix}/bin/pythonX.Y. If you intend
to install multiple versions using the same prefix you must decide which
version (if any) is your "primary" version. Install that version using
"make install". Install all other versions using "make altinstall".

For example, if you want to install Python 2.5, 2.6 and 3.0 with 2.6 being
the primary version, you would execute "make install" in your 2.6 build
directory and "make altinstall" in the others.

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

How to install multiple versions of Python in Windows?

Your questions depend a bit on "all the other software". For example, as @leiyang indicated, the answer will be different if you use conda vs just pip on vanilla CPython (the standard Windows Python).

I'm also going to assume you're actually on Windows, because on Linux I would recommend looking at pyenv. There is a pyenv-win, which may be worth looking into, but I don't use it myself because it doesn't play as nice if you also want (mini)conda environments.

1. (a) How do I install multiple Python versions?

Simply download the various installers and install them in sensible locations. E.g. "C:\Program Files\Python39" for Python 3.9, or some other location where you're allowed to install software.

Don't have Python add itself to the PATH though, since that'll only find the last version to do so and can really confuse things.

Also, you probably want to use virtual environments consistently, as this ties a specific project very clearly to a specific Python version, avoiding future confusion or problems.

1. (b) "3.8.1, 3.8.2 till 3.8.13" which should I pick?

Always pick the latest 3.x.y, so if there's a 3.8.13 for Windows, but no 3.8.14, pick that. Check if the version is actually available for your operating system, sometimes there are later versions for one OS, but not for another.

The reason is that between a verion like 3.6 and 3.7, there may be major changes that change how Python works. Generally, there will be backwards compatibility, but some changes may break how some of your packages work. However, when going up a minor version, there won't be any such breaking changes, just fixes and additions that don't get in the way of what was already there. A change from 2.x to 3.x only happens if the language itself goes through a major change, and rarely happens (and perhaps never will again, depending on who you ask).

An exception to the "no minor version change problems" is of course if you run some script that very specifically relies on something that was broken in 3.8.6, but no fixed in 3.8.7+ (as an example). However, that's very bad coding, to rely on what's broken and not fixing it later, so only go along with that if you have no other recourse. Otherwise, just the latest minor version of any version you're after.

Also: make sure you pick the correct architecture. If there's no specific requirement, just pick 64-bit, but if your script needs to interact with other installed software at the binary level, it may require you to install 32-bit Python (and 32-bit packages as well). If you have no such requirement, 64-bit allows more memory access and has some other benefits on modern computers.

2. Why does Python have such heavy dependency on the exact versions of libraries and packages etc?

It's not just Python, this is true for many languages. It's just more visible to the end user for Python, because you run it as an interpreted language. It's only compiled at the very last moment, on the computer it's running on.

This has the advantage that the code can run on a variety of computers and operating systems, but the downside that you need the right environment where you're running it. For people who code in languages like C++, they have to deal with this problem when they're coding, but target a much smaller number of environments (although there's still runtimes to contend with, and DirectX versions, etc.). Other languages just roll everything up into the program that's being distributed, while a Python script by itself can be tiny. It's a design choice.

There are a lot of tools to help you automate the process though and well-written packages will make the process quite painless. If you feel Python is very shakey when it comes to this, that's probable to blame on the packages or scripts you're using, not really the language. The only fault of the language is that it makes it very easy for developers to make such a mess for you and make your life hard with getting specific requirements.

Look for alternatives, but if you can't avoid using a specific script or package, once you figure out how to install or use it, document it or better yet, automate it so you don't have to think about it again.

3. Do virtual environments copy all the files from the main Python installation to create a virtual environment and then install specific packages inside it? Isn't that a lot of wasted resources in duplication because almost all projects require there own virtual environment.

Not all of them, but quite a few of them. However, you still need the original installation to be present on the system. Also, you can't pick up a virtual environment and put it somewhere else, not even on the same PC without some careful changes (often better to just recreate it).

You're right that this is a bit wasteful - but this is a difficult choice.

Either Python would be even more complicated, having to manage many different version of packages in a single environment (Java developers will be able to tell you war stories about this, with their dependency management - or wax lyrically about it, once they get it themselves).

Or you get what we have: a bit wasteful, but in the end diskspace is a lot cheaper than your time. And unlike your time, diskspace is almost infinitely expandable.

You can share virtual environments between very similar projects though, but especially if you get your code from someone else, it's best to not have to worry and just give up a few dozen MB for the project. On the upside: you can just delete a virtual environment directory and that pretty much gets rid of the whole things. Some applications like PyCharm may remember that it was once there, but other than that, that's the virtual environment gone.

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


Related Topics



Leave a reply



Submit