R.Zane
R.Zane

Reputation: 340

VSCode / Virtual Studio Code: Unable to Load Conda Environment in VSCode Terminal

I'm attempting to switch to VScode, to use the built-in terminal to interact with Anaconda, and know VScode supports Anaconda because it ships with Anaconda.

From the documentation, and various tutorials/videos, I see that the conda/virtual environments are recognized (seemingly natively). And yet I cannot seem to figure it out.

With the python script I want to run open in VScode, I ensure that I am using the correct interpreter/environment as follows: ctrl+shift+p to load Command Pallette Python:Select Interpreter C:...\Anaconda3\envs\p3audio\python.exe

At the bottom of the window, the environment also appears to be pointing to the environment as I loaded the p3audio environment to use python 3.7 and this is what is showing ('Anaconda Python 3.7.0') (The top Python in top level anaconda is shipped with 3.6 I believe)

When I try to run the python file by 'Run Active File' from the Terminal Menu, it automatically loads powershell and executes this into the terminal:

PS C:...\Anaconda3\envs> c:...\Anaconda3\envs\Projects\p3audo_projects\my_script.py

(the second path is in yellow)

If I try to activate the conda environment manually from the powershell terminal, it doesn't recognize it.

I can run the script fine from the anaconda prompt terminal.

I think it may be a path issue, but all of the documentation/resources I used suggest I shouldn't have the issue if I didn't select (tick the box) for add path on install. I checked my environment variable path and don't have anaconda on it.

I am wondering if it has to do with the 'you can change the default terminal shell by selecting the customize button' that loads in the terminal window. This leads me to believe that I have to manually add the conda terminal path somewhere. But this surprises me because, vscode shipped with anaconda, and I see no mention of this in the documentation.

I am obviously a newbie at this, and have suffered through environment variable/path issues many times. But I can't figure this out.

Any help would be greatly appreciated.

Thanks

Upvotes: 1

Views: 7416

Answers (7)

Brian Mac
Brian Mac

Reputation: 1

Sharing what worked for me on MacOS.

For latest versions of Anaconda (v 2019.10 at the time of this writing) add this line to your .bash_profile:

export PATH="/opt/anaconda3/bin:$PATH"

And for older versions it may look like:

export PATH="/anaconda3/bin:$PATH"

Hope that helps someone.

Upvotes: -1

forkdbloke
forkdbloke

Reputation: 1565

Please use the following settings in vs code and it ought to work, provided your conda is installed appropriately. Also, please make modifications according to your environment.

settings for powershell is mentioned below

{
"workbench.startupEditor": "newUntitledFile",
"editor.wordWrap": "on",
"editor.wordWrapColumn": 100,
"terminal.integrated.shell.windows":  "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"terminal.integrated.shellArgs.windows": [
    "-ExecutionPolicy", "ByPass", "-NoExit", "-Command", "&",
    "'C:\\Users\\kpache\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'C:\\Users\\kpache\\Anaconda3'"
    // "/K", "C:\\Users\\kpache\\Anaconda3\\Scripts\\activate.bat", "C:\\Users\\kpache\\Anaconda3"
    ],
"terminal.external.windowsExec": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",
"workbench.colorTheme": "Default Light+"
}

Upvotes: 1

sebvargo
sebvargo

Reputation: 673

For people working in Mac OS.

In order to setup the integrated terminal in VS Code to use the active Anaconda Environment's Python by default, you will need to modify your User or Workspace Setting's 'settings.json' with the following line:

"terminal.integrated.shellArgs.osx": [],

For reference: this closed issue addressed the topic.

Upvotes: 2

R.Zane
R.Zane

Reputation: 340

With the recent conda release (4.6.1) Conda can now be initialized in Powershell and can now be integrated in VSCode cleanly. I made a stackoverflow post here

Upvotes: 1

Jesse Kerr
Jesse Kerr

Reputation: 351

For anyone struggling with this, I'll tell you what worked for me. This was after four hours of trying to get Python to run in PyCharm and VS Code.

Basically dldatacowboy has the answer, but the argument for terminal.integrated.shellArgs.windows needs to be reformatted (maybe only if you're on a Windows machine?). There should be no space before the "K", and the backspaces need to be double backspaces. So my settings in my settings.json file look like so:

"terminal.integrated.shell.windows": "C:\\WINDOWS\\System32\\cmd.exe",
"terminal.integrated.shellArgs.windows": ["/K","C:\\ProgramData\\Anaconda3\\Scripts\\activate.bat C:\\ProgramData\\Anaconda3"]

Make sure to change the path to whatever comes up when you follow dldatacowboy, but then change the backslashes to double backslashes.

Upvotes: 3

R.Zane
R.Zane

Reputation: 340

Ok I figured it out. VSCode is awesome.

  1. Go to Settings (ctrl+,)

  2. Decide if you want this custom terminal to be applied at the User Settings or Workspace Settings, and select that (located just below the settings search bar)

  3. Open settings.json (don't know shortcut key yet, it is the three-dot menu located to the far right on the same row as the User Settings and Workspace Settings tabs.

  4. Locate "terminal.integrated.shell.windows" in the Default User Settings (left panel), again you can enter it into the search bar to locate it.

  5. Copy it over to the User Settings/Workspace Settings (depending on what you selected or now decide to select) (in the right panel)

e.g. copy: "terminal.integrated.shell.windows": "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe",into json array on the right

  1. Edit the path to equal your cmd executable path e.g. "terminal.integrated.shell.windows": "C:\Windows\System32\cmd.exe",
  2. You need to create the argument, which is what I was confused about: Go to start menu in windows, locate the Anaconda Prompt Open File Location with right click, inspect properties and see Target Location Path. Don't worry about the first string, copy from after the cmd.exe to the end. Use this for the "terminal.integrated.shellArgs.windows" e.g. "terminal.integrated.shellArgs.windows": [ "/K", "C:\Users\User-1\Anaconda3\Scripts\activate.bat C:\Users\User-1\Anaconda3"

Make sure you separate with a comma and properly contain strings with all four quotations. Save Close vscode. load it up and you are good to go.

Hope this helps someone! = )

Upvotes: 1

It looks like you've been mislead by the title of this article.

If you read the article, it says Anaconda comes with Visual Studio Code included. So it would be easier to setup if you download and install the Anaconda setup.

If you look at the article above, it shows in one of the installation steps, the option to install VS Code.

The following is the download link:

Upvotes: 0

Related Questions