Slashes not escaped in paths when activating conda environments in Git Bash on Windows
See original GitHub issueUpon opening a new Bash,
$ C:\ProgramData\Anaconda3\Scripts\activate mkl
bash: C:ProgramDataAnaconda3Scriptsactivate: command not found
The correct command should be using escaped backslashes or forward slashes
$ C:\\ProgramData\\Anaconda3\\Scripts\\activate mkl
$ C:/ProgramData/Anaconda3/Scripts/activate mkl
Interestingly, the command for activating the base environment is
$ C:/ProgramData/Anaconda3/Scripts/activate
Environment data
- VS Code version: 1.44.2
- Extension version (available under the Extensions sidebar): 2020.4.74986
- OS and version: Windows_NT x64 10.0.18362
- Python version (& distribution if applicable, e.g. Anaconda): Anaconda 4.8.2
- Type of virtual environment used (N/A | venv | virtualenv | conda | …): conda
- Relevant/affected Python packages and their versions: all
- Relevant/affected Python-related VS Code extensions and their versions: N/A
- Jedi or Language Server? (i.e. what is
"python.jediEnabled"
set to; more info #3977): true - Value of the
python.languageServer
setting: Microsoft
Expected behaviour
The environment is activated.
Actual behaviour
“command not found” is displayed.
Steps to reproduce:
- Ensure python.terminal.activateEnvironment to be true
- Select an Anaconda environment other than the base
- Open a new terminal
Output from Console
under the Developer Tools
panel (toggle Developer Tools on under Help
; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging
)
Activating environments other than the base
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:14: getActivatedEnvironmentVariables, Class name = _, completed in 381ms, has a truthy return value, Arg 1: undefined
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:18: Shell path 'bash'
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:18: Shell path identified as shell 'bash'
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:18: Terminal name 'bash' identified as shell 'bash'
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:18: [object Object]. Shell identified as bash (Terminal name is bash)
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:18: Shell identified as 'bash'
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:18: Get Conda environments, Class name = b, completed in 0ms, has a truthy return value, Arg 1: false, Return Value: [{"name":"base","path":"C:\\ProgramData\\Anaconda3"},{"name":"mkl","path":"C:\\Users\\user\\.conda\\envs\\mkl"},{"name":"py2","path":"C:\\Users\\user\\.conda\\envs\\py2"},{"name":"py27_32","path":"C:\\Users\\user\\.conda\\envs\\py27_32"}]
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:18: Cached data exists KeyPrefix=Cache_Method_Output_b.getCondaVersion-Args=
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:18: Cached data exists KeyPrefix=Cache_Method_Output_b.getCondaFileFromInterpreter-Args="C:\\Users\\user\\.conda\\envs\\mkl\\python.exe"-Arg-Separator-"mkl"
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:04:18: Get Conda File from interpreter, Class name = b, completed in 0ms, has a falsy return value, Arg 1: "C:\\Users\\user\\.conda\\envs\\mkl\\python.exe", Arg 2: "mkl", Return Value: undefined
Activiting the base
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:07:35: Shell path 'bash'
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:07:35: Shell path identified as shell 'bash'
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:07:35: Terminal name 'bash' identified as shell 'bash'
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:07:35: [object Object]. Shell identified as bash (Terminal name is bash)
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:07:35: Shell identified as 'bash'
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:07:35: Get Conda environments, Class name = b, completed in 0ms, has a truthy return value, Arg 1: false, Return Value: [{"name":"base","path":"C:\\ProgramData\\Anaconda3"},{"name":"mkl","path":"C:\\Users\\user\\.conda\\envs\\mkl"},{"name":"py2","path":"C:\\Users\\user\\.conda\\envs\\py2"},{"name":"py27_32","path":"C:\\Users\\user\\.conda\\envs\\py27_32"}]
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:07:35: Cached data exists KeyPrefix=Cache_Method_Output_b.getCondaVersion-Args=
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:07:35: Cached data exists KeyPrefix=Cache_Method_Output_b.getCondaFileFromInterpreter-Args="C:\\ProgramData\\Anaconda3\\python.exe"-Arg-Separator-"base"
workbench.desktop.main.js:sourcemap:262 [Extension Host] Info Python Extension: 2020-04-26 15:07:35: Get Conda File from interpreter, Class name = b, completed in 0ms, has a truthy return value, Arg 1: "C:\\ProgramData\\Anaconda3\\python.exe", Arg 2: "base", Return Value: "C:\\ProgramData\\Anaconda3\\Scripts\\conda.exe"
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:14
Top Results From Across the Web
Conda Scripts activate not working, Interpreting ...
So i use windows 10, and was configuring vscode, with git bash, ... Conda Scripts activate not working, Interpreting Backslashes as Escape ......
Read more >Git Bash for Windows showing/expecting file paths ...
When I installed Git for Windows by itself previously, it showed the slashes/colon properly, but was much more limiting and harder to use...
Read more >Release notes — conda-build 3.23.3+0.g20b08017.dirty ...
Environment activation on Windows will not work until Conda 4.1.4 is released. The activate.bat script does not look in the right place for...
Read more >How to Fix "The system cannot find the path specified. ...
It seems this error occurs when you have invalid paths in your PATH environment variable e.g. directories which don't exist anymore. To find...
Read more >How do I escape spaces in command line in Windows ...
I'm using a command that doesn't appear to allow wildcards when they apear inside quotation marks thats all! – David. May 4, 2011...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Environment Path
A user’s
%HOME%\.conda\envs
directory is the default path for creating environments on a lot of anaconda installs. (And especially for environments intended for e.g. use a a jupyter kernel, or for experimentation, this is a pretty reasonable default, even if for a larger production project you’d probably want to specify putting the project-specific environment in the project folder.)For instance, here’s the output of running a
conda create
command from git bash:My Current Workaround for This Issue
Note that this
conda
command is not actually an executable on my system path, it’s a shell function defined in this script provided by the anaconda installer. On both windows and linux, the anaconda installer presents the option to automatically add a line to the user’s.bashrc
to source this script, and in my case the relevant.bashrc
line is:Since I’m using Git Bash as my default VSC terminal, this all gets sourced when the VSC terminal starts the same way it does when I open the shell externally, and at the moment my usual workflow is precisely to run this
conda activate testenv
command after the python extension tries and fails to run its own version of the activation command:I suppose one obvious solution here would be to allow users to manually specify the full activation command they want to use rather than restricting it to just a single path (as in the current
python.condaPath
setting), and then I could instruct the extension to just run theconda activate testenv
command I currently do manually, and you might want to add this anyway even though there’s also another potential way to fix this issue that I’ve figured out.A Potential Fix
Outside if the ability to just specify a fully-custom command, there’s a few different ways that I’ve determined that the
C:\ProgramData\Anaconda3\Scripts\activate
script can be successfully run in the bash shell, the way that the extension is attempting to do:$ /c/ProgramData/Anaconda3/Scripts/activate testenv
$ 'C:\ProgramData\Anaconda3\Scripts\activate' testenv
$ "C:\ProgramData\Anaconda3\Scripts\activate" testenv
Note that the last one, double quoting, is particularly interesting, as the default windows CMD prompt also allows commands to be double-quoted, meaning that this single way of formatting it is portable across both shells.
In the windows CMD:
In windows git bash:
It seems like a straightforward solution would be to modify the way the program generates this command string, to just always include double quotes even when the path its running doesn’t seem to have any spaces or other characters that would need to be quoted/escaped under windows CMD. This probably isn’t the “proper” way to fix this problem, but adding quotes like this can only improve portability, and in this case would probably be sufficient to resolve this issue.
Based on my cursory perusal of the source code, it seems like the
getWindowsActivateCommand
function would probably be the place to add this behavior, although this is my first time looking at this codebase so I could be completely off-base here; In any case I’m not really fluent enough in typescript to be confident in proposing the code change myself.Ok, I think that’s it:
This is the logic to build the activation command if your conda version is > 4.6:
https://github.com/microsoft/vscode-python/blob/61b179b2092050709e3c373a6738abad8ce581c4/src/client/common/terminal/environmentActivationProviders/condaActivationProvider.ts#L79-L84
However
getCondaFileFromInterpreter
returnsundefined
from the logs you posted in your original comment ➡️ this logic gets bypassed ➡️ we fall back to the default Windows command, which seems to not escape slashes properly.We have 2 issues here:
getCondaFileFromInterpreter
’s logic having holes, which should be addressed when we refactor interpreter discovery in #12020 (feel free to upvote it!);I’ll update the title of this issue to focus on problem 2.
Thank you again for your patience!
Repro conditions:
conda create -p path/to/a/folder python=python3.8
)