Skip to content

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

Closed as not planned

Description

@alexzshl

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

Activity

  1. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on May 6, 2020
  2. karthiknadig commented on May 11, 2020

    @karthiknadig
    Member

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

  3. added
    needs proposalNeed to make some design decisions
    area-environmentsFeatures relating to handling interpreter environments
    bugIssue identified by VS Code Team member as probable bug
    and removed
    feature-requestRequest for new features or functionality
    on May 11, 2020
  4. karthiknadig commented on May 11, 2020

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

  5. alexzshl commented on May 11, 2020

    @alexzshl
    Author

    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

    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!

  6. karthiknadig commented on May 14, 2020

    @karthiknadig
    Member

    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.

  7. alexzshl commented on May 14, 2020

    @alexzshl
    Author

    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.

  8. JulesVerny commented on Aug 31, 2020

    @JulesVerny

    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.

  9. bwegge commented on Oct 16, 2020

    @bwegge

    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.

  10. karthiknadig commented on Oct 16, 2020

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

  11. arencambre commented on Jan 11, 2022

    @arencambre

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

  12. mkoohafkan commented on Jan 11, 2022

    @mkoohafkan

    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.

  13. jamespreed commented on Feb 18, 2022

    @jamespreed

    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
                ]
            }
    
  14. hanmala123 commented on Jun 30, 2022

    @hanmala123

    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.

  15. karrtikr commented on Dec 14, 2022

    @karrtikr

    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.

  16. locked as resolved and limited conversation to collaborators on Jan 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-environmentsFeatures relating to handling interpreter environmentsbugIssue identified by VS Code Team member as probable bugneeds proposalNeed to make some design decisions

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions