visual-studio-codeterminalanacondacondapython-venv

Conda activate failure


Sometimes when I use conda activate (my venv) inside the vs Code terminal it fails, with the following error:

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

    Traceback (most recent call last):
      File "/Users/umbertofontanazza/opt/anaconda3/lib/python3.9/site-packages/conda/exception_handler.py", line 17, in __call__
        return func(*args, **kwargs)
      File "/Users/umbertofontanazza/opt/anaconda3/lib/python3.9/site-packages/conda/cli/main.py", line 105, in main_sourced
        print(activator.execute(), end="")
      File "/Users/umbertofontanazza/opt/anaconda3/lib/python3.9/site-packages/conda/activate.py", line 199, in execute
        return getattr(self, self.command)()
      File "/Users/umbertofontanazza/opt/anaconda3/lib/python3.9/site-packages/conda/activate.py", line 165, in activate
        builder_result = self.build_activate(self.env_name_or_prefix)
      File "/Users/umbertofontanazza/opt/anaconda3/lib/python3.9/site-packages/conda/activate.py", line 343, in build_activate
        return self._build_activate_stack(env_name_or_prefix, False)
      File "/Users/umbertofontanazza/opt/anaconda3/lib/python3.9/site-packages/conda/activate.py", line 433, in _build_activate_stack
        deactivate_scripts = self._get_deactivate_scripts(old_conda_prefix)
      File "/Users/umbertofontanazza/opt/anaconda3/lib/python3.9/site-packages/conda/activate.py", line 765, in _get_deactivate_scripts
        for entry in os.scandir(join(prefix, "etc", "conda", "deactivate.d"))
      File "/Users/umbertofontanazza/opt/anaconda3/lib/python3.9/posixpath.py", line 76, in join
        a = os.fspath(a)
    TypeError: expected str, bytes or os.PathLike object, not NoneType

`$ /Users/umbertofontanazza/opt/anaconda3/bin/conda shell.posix activate base`

  environment variables:
                 CIO_TEST=<not set>
                CONDA_EXE=/Users/umbertofontanazza/opt/anaconda3/bin/conda
           CONDA_PREFIX_3=/Users/umbertofontanazza/opt/anaconda3
         CONDA_PYTHON_EXE=/Users/umbertofontanazza/opt/anaconda3/bin/python
               CONDA_ROOT=/Users/umbertofontanazza/opt/anaconda3
              CONDA_SHLVL=4
           CURL_CA_BUNDLE=<not set>
               LD_PRELOAD=<not set>
                  MANPATH=/usr/share/man:/usr/local/share/man:/Applications/Wireshark.app/Conten
                          ts/Resources/share/man:
                     PATH=/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/
                          sbin:/Applications/Wireshark.app/Contents/MacOS:/var/run/com.apple.sec
                          urity.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple
                          .security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.s
                          ecurity.cryptexd/codex.system/bootstrap/usr/appleinternal/bin
       REQUESTS_CA_BUNDLE=<not set>
            SSL_CERT_FILE=<not set>

     active environment : None
            shell level : 4
       user config file : /Users/umbertofontanazza/.condarc
 populated config files : /Users/umbertofontanazza/.condarc
          conda version : 23.9.0
    conda-build version : 3.27.0
         python version : 3.9.18.final.0
       virtual packages : __archspec=1=x86_64
                          __osx=10.16=0
                          __unix=0=0
       base environment : /Users/umbertofontanazza/opt/anaconda3  (writable)
      conda av data dir : /Users/umbertofontanazza/opt/anaconda3/etc/conda
  conda av metadata url : None
           channel URLs : https://repo.anaconda.com/pkgs/main/osx-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/osx-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /Users/umbertofontanazza/opt/anaconda3/pkgs
                          /Users/umbertofontanazza/.conda/pkgs
       envs directories : /Users/umbertofontanazza/opt/anaconda3/envs
                          /Users/umbertofontanazza/.conda/envs
               platform : osx-64
             user-agent : conda/23.9.0 requests/2.31.0 CPython/3.9.18 Darwin/22.5.0 OSX/10.16
                UID:GID : 501:20
             netrc file : None
           offline mode : False


An unexpected error has occurred. Conda has prepared the above report.
If you suspect this error is being caused by a malfunctioning plugin,
consider using the --no-plugins option to turn off plugins.

Example: conda --no-plugins install <package>

Alternatively, you can set the CONDA_NO_PLUGINS environment variable on
the command line to run the command without plugins enabled.

Example: CONDA_NO_PLUGINS=true conda install <package>

Whereas if I open the therminal app it works just fine. I noticed that Vs code tries to remember the active env for each directory and sometimes the shell promp looks like two envs were active at the same time. I guess there is a set of commands that vs code executes when I open the terminal from Vs code, where can I see them?

Any idea on how to fix this?


Solution

  • Previously terminal.integrated.inheritEnv was true by default. In 2019 they had the plan to set it to false by default. It is mentioned that this might become standard in a few VSCode versions. See https://code.visualstudio.com/updates/v1_36#_launch-terminals-with-clean-environments.

    It could be that they now changed it to be false by default, causing said problem [Needs verification].

    When false the terminal does not use a VSCode environment but instead 'fetches and use the environment of the parent process of VS Code's "main process"

    Changing terminal.integrated.inheritEnv to true again fixed the problem permanently for me.