Posh-git: Posh-Git not working correctly with Conda environments

Created on 16 Apr 2020  路  8Comments  路  Source: dahlbyk/posh-git

System Details

  • posh-git version/path: 0.7.3
  • PowerShell version: 6.2.3
  • Git version: 2.25.0.windows.1
  • Operating system name and version: Windows 7 (Microsoft Windows NT 6.1.7601 Service Pack 1)

Issue Description

I am experiencing a problem with how posh-git interacts with Conda environments. I am a Python developer and use Anaconda and use Conda to manage my environments. PowerShell is my shell of choice. I currently don't have any customization in my profile.ps1 other than the Conda activate command. This is my current profile:
(& "C:\Users\User\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | Invoke-Expression

So, the trouble is when Conda activate an environment, its name is added to my prompt in parenthesis. Now, if I do Import-Module posh-git nothing really works when in a git repo directory. Only if I remove that Conda activation code and then try to import posh-git I see that posh-git is working correctly when in a git repository directory. But when posh-git is working correctly, my Conda environments are disabled and cannot be used. That is why I require assistance on how to make both work. What changes should I make to my profile.ps1. Any help at all is highly appreciated.

P.S I don't have any proper knowledge about PowerShell scripting. So my question might be silly and easy to solve but I can't really figure out how to do it. Thanks in advance for any help

Most helpful comment

By the way, for any future reference and troubleshooting, if I try to change my PowerShell prompt again, like you know add dates and stuff to make it look better, should I do those before the Import-Module posh-git line or after it. I am guess after it as you said posh-git will not mess with existing prompt functions but still I thought I should ask.

Check out https://github.com/dahlbyk/posh-git/wiki/Customizing-Your-PowerShell-Prompt for lots of options for customizing your prompt. In general we suggest letting posh-git provide the prompt and using $GitPromptSettings (after Import-Module posh-git) to customize as you see fit.

All 8 comments

Conda and posh-git are fighting over the definition of prompt. If posh-git sees prompt has already been customized, we'll avoid overwriting it with ours.

When you have Conda initialized but not posh-git, what do you get from Get-Command prompt | % Definition?

It would also help if you can provide the output of (& "C:\Users\User\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String. In both cases you can pipe into clip to copy to your clipboard, i.e. (& "C:\Users\User\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String | clip.

Thanks for the reply. These are my outputs.
For Get-Command prompt | % Definition:

    if ($Env:CONDA_PROMPT_MODIFIER) {
        $Env:CONDA_PROMPT_MODIFIER | Write-Host -NoNewline
    }
    CondaPromptBackup;

For (& "C:\Users\User\Anaconda3\Scripts\conda.exe" "shell.powershell" "hook") | Out-String:

$Env:CONDA_EXE = "C:\Users\User\Anaconda3\Scripts\conda.exe"
$Env:_CE_M = ""
$Env:_CE_CONDA = ""
$Env:_CONDA_ROOT = "C:\Users\User\Anaconda3"
$Env:_CONDA_EXE = "C:\Users\User\Anaconda3\Scripts\conda.exe"

Import-Module "$Env:_CONDA_ROOT\shell\condabin\Conda.psm1"
conda activate base

Add-CondaEnvironmentToPrompt

Can you also share Get-Command Add-CondaEnvironmentToPrompt | % Definition?

That output might change my suggestion, but a couple preliminary options...

  1. If you Import-Module posh-git _before_ the Conda stuff runs, it might Just Work鈩笍. It looks like their module tries to backup the current prompt before applying their own, similar to how posh-git used to work. That should result in showing their stuff then the normal posh-git prompt output.
  2. There might be a way to suppress Add-CondaEnvironmentToPrompt, either within the function or by literally stripping it out before Out-String (e.g.. | ? { $_ -ne 'Add-CondaEnvironmentToPrompt' } | Out-String). Then you could use the various posh-git settings (more available if you upgrade to 1.0.0-beta4, BTW) to insert $Env:CONDA_PROMPT_MODIFIER wherever you want.

Hey! Thanks for this!! I did not have to run that command because your preliminary options worked out. It turns out you were right about the first one. I had to import posh-git _before_ Conda made any environment activation. I just added Import-Module posh-git before the Conda code and now whenever I am in a git repository, posh-git shows some beautiful git information and Conda still shows my current Python environment. Thanks for the help man.

By the way, for any future reference and troubleshooting, if I try to change my PowerShell prompt again, like you know add dates and stuff to make it look better, should I do those before the Import-Module posh-git line or after it. I am guess after it as you said posh-git will not mess with existing prompt functions but still I thought I should ask. I will close this issue asap.

By the way, for any future reference and troubleshooting, if I try to change my PowerShell prompt again, like you know add dates and stuff to make it look better, should I do those before the Import-Module posh-git line or after it. I am guess after it as you said posh-git will not mess with existing prompt functions but still I thought I should ask.

Check out https://github.com/dahlbyk/posh-git/wiki/Customizing-Your-PowerShell-Prompt for lots of options for customizing your prompt. In general we suggest letting posh-git provide the prompt and using $GitPromptSettings (after Import-Module posh-git) to customize as you see fit.

Just got another report of a posh-git/Conda issue. You might open an issue with them suggesting the Conda script be added at the end of $PROFILE instead of the beginning?

Yeah sure I will comment on that issue thread

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pharazone picture pharazone  路  8Comments

nairby picture nairby  路  6Comments

jpierson picture jpierson  路  9Comments

Patabugen picture Patabugen  路  8Comments

rkeithhill picture rkeithhill  路  5Comments