【问题标题】:How to activate conda environment in VSCode on Windows 10如何在 Windows 10 上的 VSCode 中激活 conda 环境
【发布时间】:2020-11-19 10:37:09
【问题描述】:

每当我尝试在 Windows 10 上的 VSCode 中激活 conda 时,我都会收到一条消息以初始化 shell。

我尝试了以下

  • conda init bash 并重启 VSCode
  • conda.bat activate -> 没有变化。 which python 仍然指向 /c/Users/user/anaconda3/python
  • conda init bash 后重新启动计算机
  • 通过 F1 设置解释器 > Python:选择解释器

我认为问题出在 VSCode 中,因为我可以从 cmd、powershell、gitbash 和 PyCharm 终端中激活 conda

我错过了什么吗?

$ conda activate data_science
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.

To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

康达信息

     active environment : None
       user config file : C:\Users\user\.condarc
 populated config files : 
          conda version : 4.8.3
    conda-build version : 3.18.11
         python version : 3.7.6.final.0
       virtual packages : __cuda=11.0
       base environment : C:\Users\user\anaconda3  (writable)     
           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\user\anaconda3\pkgs
                          C:\Users\user\.conda\pkgs
                          C:\Users\user\AppData\Local\conda\conda\pkgs
       envs directories : C:\Users\user\anaconda3\envs
                          C:\Users\user\.conda\envs
                          C:\Users\user\AppData\Local\conda\conda\envs
               platform : win-64
             user-agent : conda/4.8.3 requests/2.22.0 CPython/3.7.6 Windows/10 Windows/10.0.18362
          administrator : False
             netrc file : None
           offline mode : False

【问题讨论】:

    标签: python bash visual-studio-code anaconda conda


    【解决方案1】:

    您的“$”向我表明您实际上并没有打开命令行提示符,而是打开了一个替代项(可能是 bash?)。

    按 Ctrl+Shift+P 并搜索“选择默认 shell”,切换到命令行提示符,您应该可以开始了 :)

    【讨论】:

    • 显然,我正在运行 gitbash shell。更改为 bash 后,一切正常。我仍然不明白为什么 gitbash 在 VSCode 中不起作用。在外面它工作没有问题。没关系,谢谢你的帮助:)
    【解决方案2】:

    我遇到了类似的问题,但无法找到我需要的明确解决方案。我正在提供我发现对我有用的鹅卵石解决方案,如果上述答案对您不起作用,希望它可能对您有用。

    一种方法是进入注册表并更改 ExecutionPolicy(我对此没有信心)。

    对我有用的方法是在 VSCode 中按 Ctrl+Shift+P 并打开“首选项:打开设置(Json)”并添加配置文件。

    这修复了“CommandNotFoundError:您的 shell 未正确配置为使用 'conda activate'...”错误。

    注意:这里的设置是假设您使用 powershell 作为默认 shell。这也修复了一些“& ...”错误。出现错误是因为“&”是我相信的 PS(powershell)命令,如果您使用 cmd.exe 作为 shell,则可能会弹出错误。

    {...,
    "python.pythonPath": "C:\\Users\\<user>\\miniconda3\\envs\\py39\\python.exe",
    "python.condaPath": "C:\\Users\\<user>\\miniconda3\\Scripts\\conda.exe",
    "terminal.integrated.cwd": "D:\\my_VSCode_storage\\py39_env\\my_project_folder",
    "terminal.integrated.defaultProfile.windows": "PowerShell",
    "terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "icon": "terminal-powershell",
            "python.terminal.executeInFileDir": true,
            "args":["-NoLogo",
                    "-ExecutionPolicy",
                    "Bypass",
                    "-NoExit",
                    "-Command",
                    "& C:\\Users\\<user>\\miniconda3\\shell\\condabin\\conda-hook.ps1",
                    ";conda activate 'C:\\Users\\<user>\\miniconda3'"
                    ]
         },
       
         ...}
    

    【讨论】:

      【解决方案3】:

      试试这个命令source activate base,应该可以。然后可以输入命令conda activate &lt;env_name&gt;

      【讨论】:

        【解决方案4】:

        根据https://stackoverflow.com/a/69127741/11262633,执行conda init。随后的运行将正确执行对conda 的所需调用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-22
          • 1970-01-01
          • 2016-06-08
          • 2019-04-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多