You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PowerShell does not support automatic activation of conda virtual environment #11638
It seems that the powershell terminal does not support automatic activation of the conda virtual environment?I am not sure if this is my problem or it is currently the case.
If we need a way to activate the conda virtual environment from the powershell terminal, I hope the following information can provide some help
Anaconda supports powershell terminal.The following is an example of the startup command provided by Anaconda:
For anyone working on this issue: If we need to run two commands in a shell, right now there is no easy way to it. We have to use sendText and provide the command string. We need a API from VS to sendCommand, otherwise we have to duplicate shell detection and shell escaping in our extension to send the two commands correctly. Although this issue is for PS only, the activation mechanism impacts any shell that we try to send the command to.
Karthik Nadig (@karthiknadig)
Thank you for solving some of my doubts. Before I asked the question, I already knew that it was a long-standing problem.
I am not very familiar with VSCode's extension development technology. But I tried to modify the file extension.js to test the command execution
Back to VSCode, it seems to work. The conda environment is automatically activated. Perhaps the path of the condabin folder can be known through conda path, making the command applicable
However, it was so smooth that I doubted what I did wrong. Are there any potential problems with this?If you think this is a repeated and stupid idea, just tell me not to do this and close the issue, thank you!
alexzshl (@alexzshl) the right way to do this is conda init powershell followed by conda activate <name>. This is recommended by conda. One of the issues with directly referring to conda-hook.ps1 is that some times we cannot locate condabin directory. So we have to rely on conda that is available on the path. The problem with conda init powershell is that you may need to close and open your shell after running it.
As I thought, I did miss something.conda init powershell created a profile for powershell. As you said, the globalized profile may not be what everyone wants, and it does not seem to support powershellCore yet.
I have noticed that the role of profile is completely equivalent to the file conda-hook.ps1:
but the profile does not depend on the file conda-hook.ps1. The operation may be a function already built in conda.exe.Maybe the file conda-hook.ps1 was generated by conda shell.powershell hook.
I ’m not sure if I can delegate the work of the profile to extension(located at src\client\common\terminal\environmentActivationProviders\condaActivationProvider.ts):
Yeah conda, VSCode and Powershell is still a mess for the average programmer. Microsoft are now launching Windows Terminal. They make VSCode look easy, but it does not play nice with conda.
I can run python scripts from an Anaconda shell, but not from VS Code, because hat defaults to Powershell, conda .
(base) >conda init powershell, throws up errors, from my Anaconda shell (conda version 4.8.2)
UnicodeDecodeError 'utf-8' codec can't decode ... in codecs.py line 32.
I have seen suggestions to change the VS Code terminal to cmd, and avoid the use of powershell.
I am having the same issue when opening a PowerShell terminal, which just tries to run "conda activate XYZ", but fails since conda is not in the path by default. Strangely it works with a cmd terminal, so I wonder why it works there, but not for PowerShell.
Also I found that there is a dedicated setting in VS Code called "Python: Conda Path". However, no matter what path I put there, PowerShell terminals always open and try to run "conda activate XYZ" without considering the "Conda Path" setting.
We need you to set conda up on your machine before we can use it, and that includes configuring your preferred shell appropriately so that a command like conda activate will work.
As I said in my previous comment, in the case of powershell, you have to run conda init powershell from the anaconda prompt to set it up.
There are several issues that we have run into with conda, See here #5344. We even attempted using conda run to avoid activation issues, we had to revert all of that. The best option right now until we figure out a better way to integrate with conda here is what I recommend:
Turn off auto activation. To do this, disable these two settings, ideally only for the project you are working on:
Exit VS Code.
Open Anaconda Prompt or powershell (assuming you have setup conda for powershell), navigate to your project.
Activate the environment that you want.
Run code . or code project.code-workspace to launch VS Code.
This will give you an activated environment, and the extension will not have to try and activate every time a terminal is created.
This allows the script created by conda init powershell to be run on PowerShell terminal start, but is not as extreme an action as changing the entire machine- or user-specific Execution Policy.
You do get a warning that args expects an array, but wrapping "-ExecutionPolicy ByPass" in [] causes an error.
For those having issues getting PowerShell to drop into a conda environment correctly, the following settings work by calling the proper conda scripts when PowerShell launches. You will need to modify the paths to point to your condabin folder.
Python extension can automatically identify conda in cmd without any settings, "conda path" in settings seems useless in pwsh, I have tried many methods, the most convenient one is to use cmd directly.
Now that conda run is stable and VSCode provided us an alternative to activate environments: #11039, this should be fixed along with fixing the other issues. Closing this.
Activity
karthiknadig commentedon May 11, 2020
alexzshl (@alexzshl) Our recommendation for now is to activate conda first and then start code using
code .in your workspace/project folder. #10626karthiknadig commentedon May 11, 2020
For anyone working on this issue: If we need to run two commands in a shell, right now there is no easy way to it. We have to use sendText and provide the command string. We need a API from VS to sendCommand, otherwise we have to duplicate shell detection and shell escaping in our extension to send the two commands correctly. Although this issue is for PS only, the activation mechanism impacts any shell that we try to send the command to.
alexzshl commentedon May 11, 2020
Karthik Nadig (@karthiknadig)
Thank you for solving some of my doubts. Before I asked the question, I already knew that it was a long-standing problem.
I am not very familiar with VSCode's extension development technology. But I tried to modify the file extension.js to test the command execution
Back to VSCode, it seems to work. The conda environment is automatically activated. Perhaps the path of the condabin folder can be known through conda path, making the command applicable
However, it was so smooth that I doubted what I did wrong. Are there any potential problems with this?If you think this is a repeated and stupid idea, just tell me not to do this and close the issue, thank you!
karthiknadig commentedon May 14, 2020
alexzshl (@alexzshl) the right way to do this is
conda init powershellfollowed byconda activate <name>. This is recommended by conda. One of the issues with directly referring toconda-hook.ps1is that some times we cannot locate condabin directory. So we have to rely oncondathat is available on the path. The problem withconda init powershellis that you may need to close and open your shell after running it.alexzshl commentedon May 14, 2020
As I thought, I did miss something.
conda init powershellcreated a profile for powershell. As you said, the globalized profile may not be what everyone wants, and it does not seem to support powershellCore yet.I have noticed that the role of profile is completely equivalent to the file

conda-hook.ps1:but the profile does not depend on the file
conda-hook.ps1. The operation may be a function already built in conda.exe.Maybe the fileconda-hook.ps1was generated byconda shell.powershell hook.I ’m not sure if I can delegate the work of the profile to extension(located at
src\client\common\terminal\environmentActivationProviders\condaActivationProvider.ts):But it seems equally effective.

As before, I do n’t know if this is a useful idea, it depends entirely on your assessment.
JulesVerny commentedon Aug 31, 2020
Yeah conda, VSCode and Powershell is still a mess for the average programmer. Microsoft are now launching Windows Terminal. They make VSCode look easy, but it does not play nice with conda.
I can run python scripts from an Anaconda shell, but not from VS Code, because hat defaults to Powershell, conda .
(base) >conda init powershell, throws up errors, from my Anaconda shell (conda version 4.8.2)
UnicodeDecodeError 'utf-8' codec can't decode ... in codecs.py line 32.
I have seen suggestions to change the VS Code terminal to cmd, and avoid the use of powershell.
bwegge commentedon Oct 16, 2020
I am having the same issue when opening a PowerShell terminal, which just tries to run "conda activate XYZ", but fails since conda is not in the path by default. Strangely it works with a cmd terminal, so I wonder why it works there, but not for PowerShell.
Also I found that there is a dedicated setting in VS Code called "Python: Conda Path". However, no matter what path I put there, PowerShell terminals always open and try to run "conda activate XYZ" without considering the "Conda Path" setting.
karthiknadig commentedon Oct 16, 2020
We need you to set
condaup on your machine before we can use it, and that includes configuring your preferred shell appropriately so that a command likeconda activatewill work.As I said in my previous comment, in the case of powershell, you have to run
conda init powershellfrom the anaconda prompt to set it up.There are several issues that we have run into with conda, See here #5344. We even attempted using
conda runto avoid activation issues, we had to revert all of that. The best option right now until we figure out a better way to integrate with conda here is what I recommend:condafor powershell), navigate to your project.code .orcode project.code-workspaceto launch VS Code.This will give you an activated environment, and the extension will not have to try and activate every time a terminal is created.
arencambre commentedon Jan 11, 2022
Does this relate to #13464? Running into it again, and Google searches landed on this issue.
mkoohafkan commentedon Jan 11, 2022
I was able to get VSCode to recognize the conda environment without running VSCode from Anaconda prompt. This worked for me:
1: Run
conda init powershell2. add
"args": "-ExecutionPolicy ByPass"to your PowerShell terminal profile in VSCode, e.g.,This allows the script created by
conda init powershellto be run on PowerShell terminal start, but is not as extreme an action as changing the entire machine- or user-specific Execution Policy.You do get a warning that
argsexpects an array, but wrapping"-ExecutionPolicy ByPass"in[]causes an error.jamespreed commentedon Feb 18, 2022
For those having issues getting PowerShell to drop into a conda environment correctly, the following settings work by calling the proper conda scripts when PowerShell launches. You will need to modify the paths to point to your
condabinfolder.hanmala123 commentedon Jun 30, 2022
Python extension can automatically identify conda in cmd without any settings, "conda path" in settings seems useless in pwsh, I have tried many methods, the most convenient one is to use cmd directly.
karrtikr commentedon Dec 14, 2022
Now that
conda runis stable and VSCode provided us an alternative to activate environments: #11039, this should be fixed along with fixing the other issues. Closing this.