Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using a Conda environment, VS Code terminal, and the Python extension (v2023.16), a "." is inserted to the PATH env var

I'm trying to use conda, python with VS Code. The shell I'm using in the integrated terminal is PowerShell.

Everything works well in windows terminal, but after I relaunch vscode terminal, every conda commands doesn't work on vscode terminal (except activate and deactivate).


  1. conda command works at first activation Fist activtion

  2. Error After Relaunch

(dl) C:\Users\{USERNAME}\Documents\VScode Workspace\pytorch>conda env list

# >>>>>>>>>>>>>>>>>>>>>> ERROR REPORT <<<<<<<<<<<<<<<<<<<<<<

    Traceback (most recent call last):
      File "C:\Users\{USERNAME}\anaconda3\Lib\site-packages\conda\exception_handler.py", line 17, in __call__
        return func(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\{USERNAME}\anaconda3\Lib\site-packages\conda\cli\main.py", line 54, in main_subshell
        parser = generate_parser(add_help=True)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "C:\Users\{USERNAME}\anaconda3\Lib\site-packages\conda\cli\conda_argparse.py", line 127, in generate_parser
        configure_parser_plugins(sub_parsers)
      File "C:\Users\{USERNAME}\anaconda3\Lib\site-packages\conda\cli\conda_argparse.py", line 354, in configure_parser_plugins
        else set(find_commands()).difference(plugin_subcommands)
                 ^^^^^^^^^^^^^^^
      File "C:\Users\{USERNAME}\anaconda3\Lib\site-packages\conda\cli\find_commands.py", line 71, in find_commands
        for entry in os.scandir(dir_path):
                     ^^^^^^^^^^^^^^^^^^^^
    OSError: [WinError 123]  The filename, directory name, or volume label syntax is 
incorrect: '.C:\\WINDOWS\\system32'

`$ C:\Users\{USERNAME}\anaconda3\Scripts\conda-script.py env list`

  environment variables:
                 CIO_TEST=<not set>
        CONDA_DEFAULT_ENV=dl
                CONDA_EXE=C:\Users\{USERNAME}\anaconda3\condabin\..\Scripts\conda.exe
               CONDA_EXES="C:\Users\{USERNAME}\anaconda3\condabin\..\Scripts\conda.exe"
             CONDA_PREFIX=C:\Users\{USERNAME}\anaconda3\envs\dl
    CONDA_PROMPT_MODIFIER=(dl)
         CONDA_PYTHON_EXE=C:\Users\{USERNAME}\anaconda3\python.exe
               CONDA_ROOT=C:\Users\{USERNAME}\anaconda3
              CONDA_SHLVL=1
           CURL_CA_BUNDLE=<not set>
                 HOMEPATH=\Users\{USERNAME}
               LD_PRELOAD=<not set>
                     PATH=C:\Users\{USERNAME}\anaconda3\envs\dl;C:\Users\LAPTOP-
                          PNE\anaconda3\envs\dl\Library\mingw-w64\bin;C:\Users\LAPTOP-
                          PNE\anaconda3\envs\dl\Library\usr\bin;C:\Users\LAPTOP-
                          PNE\anaconda3\envs\dl\Library\bin;C:\Users\LAPTOP-
                          PNE\anaconda3\envs\dl\Scripts;C:\Users\LAPTOP-
                          PNE\anaconda3\envs\dl\bin;C:\Users\{USERNAME}\anaconda3\condabin;C:\WI
                          NDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32
                          \WindowsPowerShell\v1.0;C:\WINDOWS\System32\OpenSSH;C:\Program
                          Files\Bandizip;C:\Program Files\Microsoft SQL
                          Server\150\Tools\Binn;C:\Program Files\Microsoft SQL Server\Client
                          SDK\ODBC\170\Tools\Binn;C:\Program Files\dotnet;C:\Users\LAPTOP-
                          PNE\AppData\Local\Microsoft\WindowsApps;C:\Users\LAPTOP-
                          PNE\.dotnet\tools;C:\Users\{USERNAME}\AppData\Local\Programs\Microsoft
                          VS Code\bin;C:\Users\{USERNAME}\anaconda3;C:\Users\LAPTOP-
                          PNE\anaconda3\Library;C:\Users\{USERNAME}\anaconda3\Scripts;.C:\WINDOW
                          S\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\Win
                          dowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program
                          Files\Bandizip\;C:\Program Files\Microsoft SQL
                          Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client
                          SDK\ODBC\170\Tools\Binn\;C:\Program Files\dotnet\;C:\Users\LAPTOP-
                          PNE\AppData\Local\Microsoft\WindowsApps;C:\Users\LAPTOP-
                          PNE\.dotnet\tools;C:\Users\{USERNAME}\AppData\Local\Programs\Microsoft
                          VS Code\bin;C:\Users\{USERNAME}\anaconda3;C:\Users\LAPTOP-
                          PNE\anaconda3\Library;C:\Users\{USERNAME}\anaconda3\Scripts;
             PSMODULEPATH=C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\Windows
                          PowerShell\v1.0\Modules
         PYTHONIOENCODING=utf-8
         PYTHONUNBUFFERED=1
               PYTHONUTF8=1
       REQUESTS_CA_BUNDLE=<not set>
            SSL_CERT_FILE=C:\Users\{USERNAME}\anaconda3\envs\dl\Library\ssl\cacert.pem

     active environment : dl
    active env location : C:\Users\{USERNAME}\anaconda3\envs\dl
            shell level : 1
       user config file : C:\Users\{USERNAME}\.condarc
 populated config files : C:\Users\{USERNAME}\.condarc
          conda version : 23.7.4
    conda-build version : 3.26.1
         python version : 3.11.5.final.0
       virtual packages : __archspec=1=x86_64
                          __cuda=11.2=0
                          __win=0=0
       base environment : C:\Users\{USERNAME}\anaconda3  (writable)
      conda av data dir : C:\Users\{USERNAME}\anaconda3\etc\conda
  conda av metadata url : None
           channel URLs : https://repo.anaconda.com/pkgs/main/win-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/win-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://repo.anaconda.com/pkgs/msys2/win-64
                          https://repo.anaconda.com/pkgs/msys2/noarch
          package cache : C:\Users\{USERNAME}\anaconda3\pkgs
                          C:\Users\{USERNAME}\.conda\pkgs
                          C:\Users\{USERNAME}\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\{USERNAME}\anaconda3\envs
                          C:\Users\{USERNAME}\.conda\envs
                          C:\Users\{USERNAME}\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/23.7.4 requests/2.31.0 CPython/3.11.5 Windows/10 Windows/10.0.22621
          administrator : False
             netrc file : None
           offline mode : False

I think it's conda env or python extension problem. When conda env activated, system paths are modified appending single dot at the last of path. And Python Extension also modify system path by appending {envPath} to it. Concatanation of dot and envpath create an invalid path (i.e.,".C:\System32")

Disabling Python Extension's Activate Environment option could prevent error, but it also disable conda env auto activation.

Plus, I found that dot '.' at the last of system path doesn't removed after deactivate conda

>echo %PATH%
C:\Users\{USERNAME}\anaconda3\condabin; (...) C:\Users\{USERNAME}\anaconda3\Scripts;

>conda activate 
(base)>echo %PATH%
C:\Users\{USERNAME}\anaconda3; (...) C:\Users\{USERNAME}\anaconda3\Scripts;.

(base)>conda deactivate
>echo %PATH%
C:\Users\{USERNAME}\anaconda3\condabin;  (...) C:\Users\{USERNAME}\anaconda3\Scripts;.
like image 537
Junseo Lee Avatar asked Oct 16 '25 16:10

Junseo Lee


2 Answers

This is a bug: A drive with the name '.C' does not exist #22047.

The fix has been made in Make sure PATH ends with a separator before prepending #22046 in the pre-release channel of the Python extension. If you switch to the pre-release channel and reload VS Code and still get this problem, the maintainers have provided some more instructions to help them work out the problem.

Another (worse) solution (workaround): One user, kwikwag, has found a temporary workaround to put $env:Path = $env:Path.replace('.c','.;c') in their profile.ps1 file.

The bug was related to changes to environment variable modification code contributed by the Python extension using VS Code's shell-integration-related features and APIs like EnvironmentVariableMutator. There were recent changes to this, which you can read about at https://code.visualstudio.com/updates/v1_82#_terminal-activation-using-environment-variables. Note that you can out of that experiment by putting "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"] in your settings.json file. See also the related python.terminal.activateEnvInCurrentTerminal and python.terminal.activateEnvironment settings.

like image 153
starball Avatar answered Oct 18 '25 06:10

starball


This should be an error caused by a plugin, just use conda --no-plugins env list to temporarily disable the plugin.

like image 33
wuqx1999 Avatar answered Oct 18 '25 05:10

wuqx1999



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!