question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Slashes not escaped in paths when activating conda environments in Git Bash on Windows

See original GitHub issue

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

  1. Ensure python.terminal.activateEnvironment to be true
  2. Select an Anaconda environment other than the base
  3. 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:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:14

github_iconTop GitHub Comments

2reactions
AJMansfieldcommented, Mar 25, 2021

Environment Path

When you write .conda in your settings and interpreter path, is this a shorthand for anaconda/miniconda, or is your environment folder actually named .conda?

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:

testuser@DESKTOP-TEST MINGW64 ~
$ conda create --name testenv --yes
Collecting package metadata (current_repodata.json): done
Solving environment: done

## Package Plan ##

  environment location: C:\Users\testuser\.conda\envs\testenv



Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate testenv
#
# To deactivate an active environment, use
#
#     $ conda deactivate


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:

. /c/ProgramData/Anaconda3/etc/profile.d/conda.sh

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:

testuser@DESKTOP-TEST MINGW64 ~
$ C:\ProgramData\Anaconda3\Scripts\activate testenv
bash: C:ProgramDataAnaconda3Scriptsactivate: command not found

testuser@DESKTOP-TEST MINGW64 ~
$ conda activate testenv
(testenv) 
testuser@DESKTOP-TEST MINGW64 ~
$ python whatever.py

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 the conda 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:

  • Converting it to a unix-style path: $ /c/ProgramData/Anaconda3/Scripts/activate testenv
  • Placing the windows-style path in single quotes: $ 'C:\ProgramData\Anaconda3\Scripts\activate' testenv
  • Placing the windows-style path in double quotes: $ "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:

C:\Users\testuser>"C:\ProgramData\Anaconda3\Scripts\activate" testenv

In windows git bash:

testuser@DESKTOP-TEST MINGW64 ~
$ "C:\ProgramData\Anaconda3\Scripts\activate" testenv

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.

2reactions
kimadelinecommented, Jun 9, 2020

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 returns undefined 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!);
  • incorrect escaping of the backslashes, which I could also reproduce.

I’ll update the title of this issue to focus on problem 2.

Thank you again for your patience!


Repro conditions:

  • Windows, Git Bash as my terminal, conda 4.8.2
  • Create a conda environment with a prefix (conda create -p path/to/a/folder python=python3.8)
  • Set the interpreter to be the executable from this conda environment
  • Open terminal and watch auto-activation fail:

image

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found