Python version not changing in Vscode - python

I was creating a virtual environment in python 3.7.9 but vscode is creating an environment in python 3.9.12 (I have anaconda installed with that version). I used the command ctrl+shift+p to change the environment to 3.7.9 but it's created in 3.9.12. I had to uninstall anaconda to create an environment. How to fix this?

Steps to change the Python version in VS Code:
change to your project path (e.g. using cd)
create a new virtual environment (venv) using your Python binary in the given version (e.g. python3.7):
`which python3.7` -m venv .venv
change your VS Code interpreter by clicking in the status-bar (see red rectangle below)

There is a corresponding relationship between Python version and CONDA version.
A simple modification of interpreter cannot change the python version. You must install the CONDA environment of the corresponding version of Python.
Here is the download page of conda

Related

How to change python version in r-reticulate environment?

I would like to upgrade my python version in my r-reticulate environment. First I activate the right environment like this:
conda activate /Users/quinten/Library/r-miniconda/envs/r-reticulate
Let's check the python version:
python3 --version
Python 3.7.11
So I installed the newest python version for macOS which is 3.11.1. After that, I tried to change the Python version like described here (How to change Python version of existing conda virtual environment?) using this:
conda install python=3.11
This was successfully installed, but when I check again the version python3 --version it still returns 3.7.11. So I was wondering if anyone knows how to change the python version in an r-reticulate environment? I would like to use this in ‘Quarto’.
One shouldn't need a system-level Python - it really only complicates working with Conda environments. Rather than changing an environment's Python, it is more straightforward to create the environment with the desired specifications at the outset. For example, something like
library(reticulate)
conda_create("myenv", python_version="3.11")

How to pick python 3.11 as a conda environment in vs code

I am just start using vscode to write python code. And I am normally using conda as my package and environment tool.
I am trying to create a conda environment for python 3.11 in my project directory based on the instruction in the vscode official document https://code.visualstudio.com/docs/python/environments.
However, it only list python 3.7 - 3.10 as the python version I could choose.
I am wondering how could I pick python 3.11 in the conda environment?
I am using an Intel 2019 macbook pro. I already installed python3.11 through homebrew from command line.
And I have also installed miniconda and created a python 3.11 environment through command line.
However, when I tried to create .conda environment in vscode
Screenshot to create python environment
Screenshot to select python interpreter version for conda environment
It only show python 3.7 - 3.10
This problem is related to conda, but it's not a vscode problem. You can create a conda environment with the following command,
conda create --name myenv -c conda-forge python=3.11
Then select the created conda interpreter in the Select Interpreter panel.
More information on conda commands.
Right now, python 3.11 is still quite new so it's not yet available through the "standard" channels. You should still be able to install it with conda from the command line:
conda create -c conda-forge -p ./.venv python=3.11
-c: Adds the "conda-forge" channel: https://anaconda.org/conda-forge/python
-p: Creates the virtial environment to the given path

Python versions are not changing despite activating virtual environment in WSL2

Background:
In WSL2 (ubuntu 20.04) I created a python virtual environment inside a directory. Using the command python3 -m venv venv my system's python version was set to python3.11 (after downloading) via sudo update-alternatives --config python3 and then choosing the version. I noticed I was having some errors of missing modules when I started WSL2 (happening after a computer restart), I read this was because I was using a different python version than the one ubuntu 20.04 came with so I switched back to 3.8 via the config menu as before. I am also using VS code that's connected to my WSL2.
These are some of the contents of my venv directory: venv/bin/python venv/bin/python3 venv/bin/python3.11 venv/bin/pip venv/bin/pip3
Question:
After activating my virutal env via source venv/bin/activate, when I do python3 --version I still get a version of 3.8.10 despite creating the virtual environment with 3.11. I was able to get the interpretor set to 3.11 on VS code.I know I was in the virtual environment since my command prompt had (venv) in front. I went into the python console while in the virtual env and did import sys and sys.path this was my output ['', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload']. Why isn't the python version changing, am I misunderstanding something or did I not do something correctly? Seems like pip isn't working either but works when I switch my system python to 3.11 (I tried installing it on 3.8 but it said it was already installed).
Solved:
Answered below, just re-created the virtual env while making sure my system python version was 3.11 (may have been some mixup earlier).
By changing the selection in sudo update-alternatives --config python3 you change the selected python version also for the chosen vitrual environment (at least when using venv, it might be different with other tools).
That can cause issues, because when creating a new virtual environment envname using venv from a specific python version xx.xx, a directory named pythonxx.xx is created in /envname/lib/, and inside it a directory named site-packages that contains the packages installed by the pip of this specific environment.
So changing back to the original python version of the environment through sudo update-alternatives --config python3 should solve the issue, and probably the errors of missing modules are due to the incompatibility of the current selected python version with the original version which you installed the virtual environment from.
Personally, to avoid confusing, I name my virtual environments with the python version as a suffix, e.g envname_py3.11.1. But there might be a better method which I am not aware of.
I deleted my venv directory and recreated my virtual environment while on python3.11. This has resolved my issue.

Python version in Programs and Features is different to that in Anaconda

I have Windows 10 Home 64-bit. In the Control Panel > Programs and Features menu, I can see that I have Python 3.5.2 (Anaconda 4.1.1 64-bit) installed. As I wanted to upgrade Python 3.5 to Python 3.6, I then ran this command in Anaconda Prompt conda install python=3.6.8. After the upgrading/installing process in Anaconda Prompt was finished, I ran this command python -V and it showed Python 3.6.8 :: Anaconda 4.1.1 (64-bit). However, when I refreshed the Control Panel > Programs and Features menu, it still showed Python 3.5.2 (Anaconda 4.1.1 64-bit). I then ran this command conda info --envs and it showed I only have the base environment.
Could anyone please advise:
Why are there two different information of Python versions in this case?
Which Python version is actually installed in my laptop?
In my Jupyter Lab Launcher, under Notebook section, I can see two Python symbols: Python 3 and Python [conda env:root]*. What are the differences between these two and which one should I use to launch a new notebook?
Thankyou!
Conda creates virtual environment inside your machine. The python version is different inside conda. If you want install different python inside conda you can decide the python at the time of creation of environment.
conda create -n TestEnv python=3.6.8
If you want update the python inside conda virtual environment, activate the virtual environment using
conda activate TestEnv
Now you can see the python version of your virtual environment
Deactivate your virtual environment and check python version using python -V this is the version of your Laptop Python or base version of python.

How to change python version in Anaconda?

I am trying to get into deep learning. I installed Anaconda to use jupyter and generally not to care about installing all of those packages like matplotlib etc myself. But I cannot install tensorflow as it works only with Python 3.4, 3.5, or 3.6 but I have 3.7. After I read about it I installed python 3.6.8. I uninstalled Anaconda and installed it again, nothing changed. After that, I used this command
conda install python=3.6.8
to presumably install python 3.6.8 for it (I found this solution somewhere on the web). The command worked but didn't change anything. Please help
A better (recommended) alternative is to create a virtual environment of the desired Python version and then use that environment to run Tensorflow and other scripts.
To do that, you can follow the instructions given here.
BUT, if you don't want to create a separate environment, then conda install python=<version> should do.
OR (not recommended) you can download the "latest" Anaconda installer with your required Python version bundled.
Source
Sometime command is not working as expected I was also facing same issue when I
used this command.
conda install python=<version>
then I have changed the Python version with the help of anaconda Navigator. I have create new enviornment and follow below instruction=>
In Navigator, click the Environments tab, then click the Create button. The Create new environment dialog box appears.
In the Environment name field, type a descriptive name for your environment.
3.In the Packages list select “Python” and in the Python version list select the version you want to use.
4.Click the Create button.
5.Navigator creates the new environment and activates it, as shown by the highlighted green bar. All actions take place in the active environment.
for more details please go through this link =>
https://docs.anaconda.com/anaconda/navigator/tutorials/use-multiple-python-versions/
By default, the conda environment will use the python version 3.7, since you installed Anaconda with python3.7.
You would need to create a symbolic link to the new version of the python (in your case python3.6.8) using
ln -s ~/anaconda3/bin/<python3.6.8>
(you may need to replace 'python3.6.8' with the appropriate file name).
Another, better way would be to create a new environment in conda and then use python3.6.8 as you would like. Follow steps at https://conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#creating-an-environment-with-commands to create an environment.
For python3.6.8, run the following command:
conda create -n <myenv> python=3.6.8
(replace 'myenv' with the name of your new environment)
After that, activate the environment with :
conda activate <myenv>
This is a bug of the macOS 10.14.6. I degrade the python version from 3.7.4 to 3.7.0 in Anaconda and it works. Here I share my solution. I solve this question in other website. Check the answer by clicking here(https://www.udemy.com/course/the-python-mega-course/learn/lecture/4775342#questions/11049798).
You just can change the python version by creating a new environment in anaconda.
It will ask for the python version when you create an environment
The most pain-free approach that worked for me was to install the legacy version of Anaconda. If you want Python 3.6 as default, I recommand downloading Anaconda 5.2.0 which was the last version to use Python 3.6 in base environment as default. (Legacy versions can be found here.)
If you also want the latest version of Anaconda, there is an easy way. With Anaconda 5.2.0 installed, you can actually update to newer versions of Anaconda by
conda update anaconda
which will try to update all your packages except keepping Python at 3.6.x.

Categories