ChatGPT解决这个技术问题 Extra ChatGPT

Activating Anaconda Environment in VsCode

I have Anaconda working on my system and VsCode working, but how do I get VsCode to activate a specific environment when running my python script?

Most of the answers are windows specific. It would be nice to have an answer that includes MacOS and Linux systems.

d
dumbledad

Simply use

shift + cmd + P Search Select Interpreter

https://i.stack.imgur.com/TsyCD.png

Select it and it will show you the list of your virtual environment created via conda and other python versions

https://i.stack.imgur.com/Koan0.png

select the environment and you are ready to go.

Quoting the 'Select and activate an environment' docs

Selecting an interpreter from the list adds an entry for python.pythonPath with the path to the interpreter inside your Workspace Settings.


This works. However, when I try to run the script using the CodeRunner extension, it doesn't seem to recognize that I am in a conda environment and gives me an error saying module doesnt exist
Yeah this doesn't seem to give the terminal session a path variable or set the system or user PATH variable, even temporarily in a virtual environment.
This also doesn't seem to affect the environment used when running code cells in the Jupyter window
It works but there's another problem with my case! When I select the "python: select interpreter" option, it doesn't show me all the anaconda environments! Actually it just show 2 of them that I don't want. Have anyone any idea?
@malthe Go to the Anaconda Navigator and in the Home tab choose your venv right next to Application on label and then launch VS code from the Navigator (if instead of launch option it's showing install option, install it first, even if you have vs code installed on your pc) Just like what Zhenyu explained below.
B
Brian Burns

If Anaconda is your default Python install then it just works if you install the Microsoft Python extension.

The following should work regardless of Python editor or if you need to point to a specific install:

In settings.json edit python.path with something like

"python.pythonPath": "C:\\Anaconda3\\envs\\py34\\python.exe"

Instructions to edit settings.json


But how do I activate a specific environment so that the script runs in that Anaconda environment?
The answer posted is how you run a .py file within VS Code using a specific environment like Anaconda. The path of my Anaconda install in the settings.json is python.pythonPath": "C:\\Anaconda3\\envs\\py34\\python.exe" note I updated to use "\\" to escape the single "\" in the path. Then right click on the .py code and choose "run file in python terminal" hope that helps
vscode now has the python.venvPath setting that allows you to use Python: Select Workspace Interpreter in the command palette to choose the env (see my answer for details). That way you don't have to edit the pythonpath every time you change envs.
This alone, settings.json, was not enough. It seemed to need also launch.json to get it working on OSX eg: "python.pythonPath": "/Users/me/miniconda2/envs/py27/bin/python"
M
MarredCheese

Setting python.pythonPath in VSCode's settings.json file doesn't work for me, but another method does. According to the Anaconda documentation at Microsoft Visual Studio Code (VS Code):

When you launch VS Code from Navigator, VS Code is configured to use the Python interpreter in the currently selected environment.

https://i.stack.imgur.com/tuUiS.png


The efficient's solution to the problem by far.
a
alexbhandari

The best option I found is to set the python.venvPath parameter in vscode settings to your anaconda envs folder.

"python.venvPath": "/Users/[...]/Anaconda3/envs"

Then if you bring up the command palette (ctl + shift + P on windows/linux, cmd + shift + P on mac) and type Python: Select Workspace Interpreter all your envs will show up and you can select which env to use.

The python extension will also need to be installed for the Select Workspace Interpreter option.

Note: The Select Workspace Interpreter takes around 10 seconds to come up on my computer using the current version of VSCode.


Ctlr + Comma for Windows opens vscode settings :) Thank you.
On Windows this worked for me: "python.venvPath": "C:\\Users\\User\\AppData\\Local\\Continuum\\anaconda3". Using VSCode 1.19.1
On more recent version the command is Python: Select Interpreter
p
pkowalczyk

Although approved answer is correct, I want to show a bit different approach (based on this answer).

Vscode can automatically choose correct anaconda environment if you start vscode from it. Just add to user/workspace settings:

{
    "python.pythonPath": "C:/<proper anaconda path>/Anaconda3/envs/${env:CONDA_DEFAULT_ENV}/python"
}

It works on Windows, macOS and probably Unix. Further read on variable substitution in vscode: here.


Thanks for the answer. On Linux, it is { "python.pythonPath": "/<proper path>/anaconda3/envs/${env:CONDA_DEFAULT_ENV}/bin/python" }. Also I have to re-open the VS Code after changing the environment.
An easy way to get the pythonpath on Mac/ Unix is to just run: conda activate <your env> && which python
C
Chris Fonnesbeck

Unfortunately, this does not work on macOS. Despite the fact that I have export CONDA_DEFAULT_ENV='$HOME/anaconda3/envs/dev' in my .zshrc and "python.pythonPath": "${env.CONDA_DEFAULT_ENV}/bin/python", in my VSCode prefs, the built-in terminal does not use that environment's Python, even if I have started VSCode from the command line where that variable is set.


D
Davide Fiocco

Just launch the VS Code from the Anaconda Navigator. It works for me.


r
redice li

Find a note here: https://code.visualstudio.com/docs/python/environments#_conda-environments

As noted earlier, the Python extension automatically detects existing conda environments provided that the environment contains a Python interpreter. For example, the following command creates a conda environment with the Python 3.4 interpreter and several libraries, which VS Code then shows in the list of available interpreters:

 conda create -n env-01 python=3.4 scipy=0.15.0 astroid babel 

In contrast, if you fail to specify an interpreter, as with conda create --name env-00, the environment won't appear in the list.


I was just going to post the same answer! It's the only correct answer in some cases! The VSCode documentation explains clearly that conda environments that don't have a python module in them will NOT be listed in the list of interpreters by VSCode. So for example, if you've just created an empty conda virtual environment without any modules in it, you won't see it listed in VSCode. So I simply did conda install --name my_env_name python and my problem is fixed. For more on this command: docs.conda.io/projects/conda/en/latest/user-guide/tasks/…
J
Jason Ridenour

If you need an independent environment for your project: Install your environment to your project folder using the --prefix option:

conda create --prefix C:\your\workspace\root\awesomeEnv\ python=3

In VSCode launch.json configuration set your "pythonPath" to:

"pythonPath":"${workspaceRoot}/awesomeEnv/python.exe"

I didn't need a separate environment, but to use my normal conda env on osx, I needed to redirect the pythonPath. The settings.json seemed to control properly the unit tests inside IDE . To get debugging to work though, I needed to also set pythonPath inside launch.json as this answer says. For me that was; "python.pythonPath": "/Users/me/miniconda2/envs/py27/bin/python"
A
Abhishek Tripathi

I found out that if we do not specify which python version we want the environment which is created is completely empty. Thus, to resolve this issue what I did is that I gave the python version as well. i.e

conda create --name env_name python=3.6

so what it does now is that it installs python 3.6 and now we can select the interpreter. For that follow the below-mentioned steps:

Firstly, open the command palette using Ctrl + Shift + P Secondly, Select Python: select Interpreter Now, Select Enter interpreter path We have to add the path where the env is, the default location will be C:\Users\YourUserName\Anaconda3\envs\env_name

Finally, you have successfully activated your environment. It might now be the best way but it worked for me. Let me know if there is any issue.


u
user3053452

Python Path is now deprecated and now you should set Conda Path instead. This way you can pick different environements on the fly.

Click ctrl + , then search for Conda Path and add absolute path to script, e.g.:

C:\Users\{myUser}\miniconda3\Scripts\conda.exe

Pick specific environment for each project in bottom left corner or through Command Pallete (ctrl + Shift + P -> search Python: Select Interpreter)


M
MarredCheese

I found a hacky solution replace your environment variable for the original python file so instead it can just call from the python.exe from your anaconda folder, so when you reference python it will reference anaconda's python.

So your only python path in env var should be like:

"C:\Anaconda3\envs\py34\", or wherever the python executable lives

If you need more details I don't mind explaining. :)


S
Siddharth Magadum

As I was not able to solve my problem by suggested ways, I will share how I fixed it.

First of all, even if I was able to activate an environment, the corresponding environment folder was not present in C:\ProgramData\Anaconda3\envs directory.

So I created a new anaconda environment using Anaconda prompt, a new folder named same as your given environment name will be created in the envs folder.

Next, I activated that environment in Anaconda prompt. Installed python with conda install python command.

Then on anaconda navigator, selected the newly created environment in the 'Applications on' menu. Launched vscode through Anaconda navigator.

Now as suggested by other answers, in vscode, opened command palette with Ctrl + Shift + P keyboard shortcut. Searched and selected Python: Select Interpreter

If the interpreter with newly created environment isn't listed out there, select Enter Interpreter Path and choose the newly created python.exe which is located similar to C:\ProgramData\Anaconda3\envs\<your-new-env>\ . So the total path will look like C:\ProgramData\Anaconda3\envs\<your-nev-env>\python.exe

Next time onwards the interpreter will be automatically listed among other interpreters.

Now you might see your selected conda environment at bottom left side in vscode.


q
questionto42standswithUkraine

"python.pythonPath" is deprecated, quote from vscode:

The "python.pythonPath" setting in your settings.json is no longer used by the Python extension. If you want, you can use a new setting called "python.defaultInterpreterPath" instead. Keep in mind that you need to change the value of this setting manually as the Python extension doesn’t modify it when you change interpreters. Learn more.

Thus, IF you want to assign the path manually (not reccommended, as explained above), open the "settings.json" of your workspace or the default one and use

{
"python.defaultInterpreterPath": "C:\\Users\\MYUSER\\anaconda3\\envs\\testenv\\python.exe"
}

b
bhola prasad

The simplest way to do it -

First open up terminal or command line and navigate to the project directory where you created the virtual environment. Then activate the virtual environment with the command conda activate venv_name Once activated, in terminal type - code .

This will open the vscode with the activated virtual environment. Look at the bottom of the pic. The dot after code . tells terminal to open the current directory in vscode.

https://i.stack.imgur.com/IR9nH.jpg


This did not deserve to be downvoted. It is a workaround, but having a working environment is the basis in debugging the configuration problem asked byt the OP.