[Experimental Enhancement] Improve the integration and use of micromamba with the Powershell terminal in PyCharm
Opened this issue · 1 comments
The following fix works well locally with PyCharm 2023.3.4
+ micromamba 1.5.6
Problem Description
Under the latest commit of micromamba-pycharm
(665a51a), after selecting the micromamba
environment as the interpreter for a PyCharm project, the integrated terminal of PyCharm fails to activate the corresponding micromamba
environment and displays the following error:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows
Failed : The term 'Failed' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Failed to activate conda environment.
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (Failed:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Please : The term 'Please' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:2 char:2
+ Please open Anaconda prompt, and run `conda init powershell` there.
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (Please:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Relevant items from the PyCharm log (idea.log
) are as follows:
2024-02-29 16:56:53,289 [ 5285] WARN - #c.i.p.t.PyVirtualEnvTerminalCustomizer - Can't find null, will not activate conda
Cause of the Problem
When activating the terminal in Powershell, PyCharm first looks for and uses conda.bat
(equivalent to micromamba.bat
in micromamba) to complete the activation, then constructs the following command to activate the environment in Powershell that matches the current conda project environment1:
& '($MAMBA_ROOT_PREFIX\condabin\conda.bat)' shell.powershell hook | Out-String | Invoke-Expression ;
try {
conda activate '(current project conda environment path)'
} catch {
Write-Host('Failed to activate conda environment.
Please open Anaconda prompt, and run `($MAMBA_ROOT_PREFIX\condabin\conda.bat) init powershell` there.')
}
Solution to the Problem
Clearly, we need to:
- Ensure that the file
conda.bat
exists under$MAMBA_ROOT_PREFIX\condabin
, allowing PyCharm to complete the subsequent activation steps. - Ensure that the
conda.bat
file can executeshell.powershell hook
. - Ensure that after completing
shell.powershell hook
, the alias for conda in the current environment points to micromamba, to facilitate the finalconda activate '(current project conda environment path)'
.
Given this, the best solution currently seems to be creating a conda.bat
script under $MAMBA_ROOT_PREFIX\condabin
that can accomplish the above points.
However, this solution also has significant drawbacks:
- It requires creating a
conda.bat
in the user's$MAMBA_ROOT_PREFIX
. - Since it needs to point conda to micromamba after completing
shell.powershell hook
, it's not very user-friendly.
Note 1: Finally, if the repository owner is willing, I can submit a PR for this part, otherwise, this issue can be kept as a reference for users with the same problem who are keen to solve it manually.
Note 2: This problem does not exist on Linux systems, as PyCharm only activates the specific environment corresponding to the current interpreter on Windows2.
Footnotes
-
See: intellij-community/python/python-terminal/src/com/intellij/python/terminal/PyVirtualEnvTerminalCustomizer.kt at master · JetBrains/intellij-community (github.com),intellij-community/python/python-sdk/src/com/jetbrains/python/packaging/CondaExecutablesLocator.kt at master · JetBrains/intellij-community (github.com),intellij-community/python/python-sdk/src/com/jetbrains/python/packaging/PyCondaPackageService.kt at master · JetBrains/intellij-community (github.com) ↩
-
See: https://github.com/JetBrains/intellij-community/blob/9eac5d7b58f099b55962ca043f889f5dd4541830/python/python-terminal/src/com/intellij/python/terminal/PyVirtualEnvTerminalCustomizer.kt#L138 ↩
Your proposal doesn't sound too complicated. The bat file could look something like this:
- If command is
"shell.powershell hook"
- Call micromamba to get the actual hook string
- Potentially modify the hook string to overwrite the
conda
binary/function - Invoke the (modified) hook string
How is PyCharm calling $MAMBA_ROOT_PREFIX\condabin\conda.bat
related to the user selecting a specific Conda binary? Does PyCharm always call $MAMBA_ROOT_PREFIX\condabin\conda.bat
no matter the value of conda.bat
, or does it call the binary specified by the user?