Why there are two python versions in a virtual environment? - python

I have created a virtual environment using anaconda in VS. When the environment is active. I check the version using python --version, it gives the following output Python 3.9.9, whereas when I use which python and check the version from the path /usr/bin/python --version I get a different version Python 2.7.18. Why is that happening, and which version does the environment use?

Once your virtual environment is activated the python command will use the python version from your venv (located in path/to/.venv/bin/python).
which python and /usr/bin/python forces the use of the python version installed in /usr/bin/python which in your case seems to be version 2.7.18. If you want to change your default Python version (the one thats used with python without a venv being active) you can use sudo ln -s /usr/bin/python /usr/bin/python3.9.

Related

How to change python3 version on mac to 3.10.10

I am currently running python 3.9.13 on my mac. I wanted to update my version to 3.10.10
I tried running
brew install python
However it says that "python 3.10.10 is already installed"!
When i run
python3 --version
in the terminal it says that i am still on "python 3.9.13"
So my question is, how do i change the python version from 3.9.13 to 3.10.10? I already deleted python 3.9 from my applications and python 3.10 is the only one that is still there.
I also tried to install python 3.10.10 from the website and installing it. However it does not work. Python 3.10.10 is being installed successfully but the version is still the same when i check it.
Python 3.10.10 is already installed along with Python 3.9.13. Your path is probably pointing to 3.9.13 and that's why you're getting that Python version.
Try modifying your path variable to point to brew's Python installation, or (better yet) make use of a virtual environment.
a) Telling what Python version your PATH variable is pointing to:
$ which python
Will tell you what's the actual executable's path. That way, you are going to see exactly where you main python resides.
b) If you want to create a virtual environment with brew's python try something like:
$ /usr/local/Cellar/python#3.10/3.10.1/bin/python3 -m venv py310
$ source venv/bin/activate
$ python
> # you should be inside a 3.10.1 envornamente
> CTRL+D
$ pip install requests
you can use pyenv to work with multiple python environments
things to do:
install pyenv : brew install pyenv
install particular python: pyenv install 3.10.10
set python3.10.10 to gloabal python env: pyenv global 3.10.10
and can start using python 3.10.10 version
Just delete the current python installation on your device and download the version you want from the offical website. That is the easiest way and the most suitable one for a beginner.

Virtual environment (venv) gives a different python version from interpreter

In VS Code (Windows), my python interpreter points to version 3.11.
python -V in terminal gives me Python 3.11.0
I create a virtual environment with python3 -m venv virtual called virtual, and activate it with .\virtual\Scripts\activate.
Now in my environment, checking python -V gives me Python 3.9.13 instead.
How do I get venv to create a Python 3.11 environment?
You are creating virtual environment with python3 but checking version with just python. I want you to check if python and python3 are pointing same python executable file before creating virtual environment.
May be try creating virtual environment using just python, since its version is 3.11.0.
$ python -m venv virtual
Or you can create virtual environment with specifing the path of your python 3.11 executable file.
$ C:/path/to/your/python3-11-execuatable/python.exe -m venv virtual
In the view tab above click on Comand palette> python interpreter from there you can select python 3.11, otherwise you can add it by clicking on enter interpreter path and then select the path where python 3.11 is located at

MacOS python command not finding active Conda environment from VS Code terminal

I recently got a new M1 MacBook - first time ever using a Mac - and immediately downloaded Miniconda to get it set up for some Python work.
I created some virtual environments (e.g. conda create -n myenv python=3.8) but when active, the python command defaults to Mac's Python 2.7, preventing me from running scripts from the command line in VS Code. For example:
conda activate myenv
(myenv)% which python
/usr/bin/python
where shows me the default 2.7 installation and the correct virtual environment version, but I can't access it.
(myenv)% where python
/usr/bin/python
/Users/user/miniconda3/envs/myenv/bin/python
Any idea how to get VS Code to find the proper Python version? It seems to work from the built in terminal, just not VS Code.
OS: Monterey 12.2.1
VS Code: 1.65.0
Miniconda: 4.10.1
Could you try to take advantage of python3 instead of python in the MacOS?
Or you can try to rename the python.exe to something others, such as python2.7 under the /usr/bin/python?
So the problem is, that in windows, you can change the path, but in mac there is this thing, that if you don't select the version, for example:
sudo python test.py
It will run Python 2.7, because it is installed, and it runs the lowest installed version. So try using
sudo python3 test.py
or specify the version in the terminal
sudo python3.8 test.py

Remove Python from usr/local/bin - 2 versions installed

Somehow i have 2 versions of Python 2 installed. The one installed in usr/local/bin is Python 2.7.13 and the one in usr/bin is Python 2.7.6 - i want to keep the 2.6 version and remove the other one.
How do i remove the other one safely ?
Output of which python
/usr/local/bin/python
Also, the symlink points to python2.7 in the usr/local/bin itself.
EDIT: The other version is not 2.6 but 2.7.6, which should be the default version installed.
The default python version on your server is python2.7. If you remove it, you will break your system. Some apps use python2.7 to work.
If you really need to keep python2.6 (old python script for example), just specify the path to python2.6 when executing a script :
/usr/bin/python2.6 some_old_script.py
You can create virtualenv too :
virtualenv -p /usr/bin/python2.6 my_venv
cd my_venv
source bin/activate
Now your venv is active, if you run :
python some_old_script.py
The script will be run in python2.6
To exit the venv :
deactivate
EDIT
Using multiple python versions on the same system is not a problem. It's oftenly required (python2.6, 2.7, 3 etc.)
If by mistake you installed multiple python versions (in our case python2.7.6 and python2.7.13), you can use aliases to point to the version you want to use by default :
alias python=/your/python/path/python2.7.6

Upgrading Python on Ubuntu 15.10

I'm trying to upgrade my python to 2.7.11 on Ubuntu 15.10, by following the guides here
http://tecadmin.net/install-python-2-7-on-ubuntu-and-linuxmint/
http://mbless.de/blog/2016/01/09/upgrade-to-python-2711-on-ubuntu-1404-lts.html
But after, when I try and reopen terminal, and type in python it still shows the version is 2.7.10
Does anyone know why this is the case?
The post you've linked says explicitly in the first sentence: "you should not touch the Python version of the system." i.e., /usr/bin/python should remain the same and therefore if /usr/bin is earlier in your $PATH envvar than the path to the newly installed python version then python invokes /usr/bin/python and you see the old version.
To install/manage multiple minor python versions, you could use pythonz or similar tools (such as pyenv):
$ pythonz install 2.7.11 # to install 2.7.11 version
$ $(pythonz locate 2.7.11) # to start the corresponding version
You could create a virtualenv using the desired python version (using virtualenvwrapper's command):
$ mkvirtualenv -p $(pythonz locate 2.7.11) py2.7.11
python will refer to 2.7.11 version inside the virtual environment.

Categories

Resources