2

I have python 3.7.8 and python 3.10.0 installed. I have a question regarding the dependencies of these two. Is it okay to have this two installed at the same time but in different versions?

Does it make my site-packages conflict with one another?

On my command prompt, when I check my python version it says I am using the recent version, 3.10.0.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • What version is the default? – Phix Dec 04 '21 at 03:28
  • 2
    I have 5 different versions right now, and no issues. You might want to instead read about creating virtual environments using a specific python version and [Dealing with multiple Python versions and PIP?](https://stackoverflow.com/q/2812520/2745495) because the more useful question than "is it okay?" is "how to ensure I use the correct python version?" – Gino Mempin Dec 04 '21 at 03:31
  • @Phix the default is 3.10.0 . Would I uninstall the python version 3.7.8? – Flakes Monster Dec 04 '21 at 03:47
  • @GinoMempin Ohh now I get it. Thanks for answering my questions! Big Help! – Flakes Monster Dec 04 '21 at 03:48
  • 1
    Are you on Windows or a non-Windows system? The correct answer is very different for Windows. – ShadowRanger Dec 04 '21 at 04:37

2 Answers2

1

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
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
0

To add onto Gino Mempin's answer it's also worth noting that in Windows you can quickly switch between versions of Python by editing the PATH environment variable in System Variables under your Environment Variables.

From my experience most people who switch between Python versions are on Linux but for all those people who might be using Windows you can just go to This PC -> Right-click -> Properties -> Advanced System Settings then from System Properties go to the Advanced tab and select Environment Variables to edit PATH. Note you don't have to delete older versions of Python here you can just add other ones and then select to move them above the others to give them priority.

You'll also need to restart your Command Prompt or Powershell after doing this if you had it open and was using Python before editing the environment variables.

After doing this you'll be able to install and manage packages for the current version of Python that has priority.

ZeroDayTea
  • 16
  • 4