Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerShell does not support automatic activation of conda virtual environment #11638

Closed
alexzshl opened this issue May 6, 2020 · 13 comments
Closed
Assignees
Labels
area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug needs proposal Need to make some design decisions

Comments

@alexzshl
Copy link

alexzshl commented May 6, 2020

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:

%windir%\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -NoExit -Command "& 'F:\ProgramData\Anaconda3\shell\condabin\conda-hook.ps1' ; conda activate 'F:\ProgramData\Anaconda3' "

According to the above information, I activated the conda virtual environment in the powershell terminal of VSCode

image

@alexzshl alexzshl added triage-needed Needs assignment to the proper sub-team feature-request Request for new features or functionality labels May 6, 2020
@karthiknadig karthiknadig self-assigned this May 6, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label May 6, 2020
@karthiknadig
Copy link
Member

@alexzshl Our recommendation for now is to activate conda first and then start code using code . in your workspace/project folder. #10626

@karthiknadig karthiknadig added needs proposal Need to make some design decisions area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug and removed triage feature-request Request for new features or functionality labels May 11, 2020
@karthiknadig
Copy link
Member

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
Copy link
Author

alexzshl commented May 11, 2020

@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

async getPowershellCommands(e){return[`& 'F:\\ProgramData\\Anaconda3\\shell\\condabin\\conda-hook.ps1' ; conda activate 'F:\\ProgramData\\Anaconda3' ; conda activate ${e.toCommandArgument()}`]}

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

image

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
Copy link
Member

@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.

@alexzshl
Copy link
Author

alexzshl commented May 14, 2020

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:
image

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):

async getPowershellCommands (e) {return [`(& "F:\\ProgramData\\Anaconda3\\Scripts\\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression ; conda activate ${e.toCommandArgument()}`]}

But it seems equally effective.
image

As before, I do n’t know if this is a useful idea, it depends entirely on your assessment.

@luabud luabud assigned luabud and unassigned karthiknadig Aug 12, 2020
@JulesVerny
Copy link

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
Copy link

bwegge commented 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
Copy link
Member

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:

  1. Turn off auto activation. To do this, disable these two settings, ideally only for the project you are working on:
    image
  2. Exit VS Code.
  3. Open Anaconda Prompt or powershell (assuming you have setup conda for powershell), navigate to your project.
  4. Activate the environment that you want.
  5. 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.

@arencambre
Copy link

Does this relate to #13464? Running into it again, and Google searches landed on this issue.

@mkoohafkan
Copy link

mkoohafkan commented 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 powershell
2. add "args": "-ExecutionPolicy ByPass" to your PowerShell terminal profile in VSCode, e.g.,

    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell",
            "args": "-ExecutionPolicy ByPass"
        }

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.

@jamespreed
Copy link

jamespreed commented 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 condabin folder.

    "python.condaPath": "C:\\Anaconda3\\condabin\\conda.bat",
    "terminal.integrated.defaultProfile.windows": "PowerShell",

    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "path": "C:\\Program Files\\PowerShell\\7\\pwsh.exe",  // using powershell 7
            "icon": "terminal-powershell",
            "args": [
                "-ExecutionPolicy", 
                "ByPass", 
                "-NoLogo",
                "-NoExit",
                "-Command",
                "& 'C:\\Anaconda3\\condabin\\conda_hook.bat'; conda init powershell | Out-Null"  // direct to null, conda init is noisy
            ]
        }

@hanmala123
Copy link

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
Copy link

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.

@karrtikr karrtikr closed this as not planned Won't fix, can't repro, duplicate, stale Dec 14, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug needs proposal Need to make some design decisions
Projects
None yet
Development

No branches or pull requests

10 participants