本文介绍了路径未从 VSCode 终端中的 conda activate 正确更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VSCodes 终端面板.我激活了一个 conda 环境.出于某种原因,python 命令仍然设置为 /usr/bin/python,而不是 conda 环境的正确路径.

I am using VSCodes terminal pane. I activate a conda environment. For some reason, the python command is still set to /usr/bin/python, instead of the correct path to the conda environment.

% conda activate myenv
% which python
/usr/bin/python

正确的 anaconda 环境目录似乎确实在 $PATH 变量中,但 /usr/bin 似乎优先于它.

The correct anaconda environment directory does seem to be in the $PATH variable, but /usr/bin seems above it in priority.

当我通过操作系统打开标准终端时,行为与我预期的一样.

When I open a standard terminal through the OS, the behavior is as I expect.

% conda activate myenv
% which python
/Users/cpl/anaconda3/envs/myenv/bin/python

请注意:我已经将 VSCode 首选项键 python.pythonPath 设置为 /Users/cpl/anaconda3/envs/myenv/bin/python,我认为它可以正常工作.当我通过右键单击并选择 Run Python File In Terminal 运行文件时,会执行正确的 Python(来自 conda 环境).我的问题是直接使用VSCode终端执行python.

Please note: I have already set the VSCode preferences key python.pythonPath to /Users/cpl/anaconda3/envs/myenv/bin/python, and I think that it works correctly. When I run a file through right-clicking and selecting Run Python File In Terminal, the correct python (from the conda environment) is executed. My problem is using the VSCode terminal directly to execute python.

我的 shell 是 zsh,我使用的是 OSX.有什么建议吗?

My shell is zsh, and I am using OSX. Any advice?

推荐答案

VSCode 文档中解释了此行为:为什么终端的 $PATH 环境变量中有重复的路径和/或为什么它们被反转了?#

This behavior is explained in the VSCode docs:Why are there duplicate paths in the terminal's $PATH environment variable and/or why are they reversed?#

听起来 VSCode 会在 MacOS 中运行您的 .zshrc 两次,这与 conda 生成的 PATH 变量定义相冲突.

It sounds like VSCode will run your .zshrc twice in MacOS, conflicting with the conda-generated PATH variable definitions.

上面的链接中列出了两种解决方案.对我有用的是设置 VSCode 设置 "terminal.integrated.inheritEnv": false.该文档警告说,如果您这样做,您的所有环境变量都将被剥离.但是,我发现我仍然在 .zshrc 文件中定义了我的自定义变量.

There are two solutions listed in the link above. The one that works for me is to set the VSCode setting "terminal.integrated.inheritEnv": false. The documentation warns that all of your environmental variables will be stripped if you do this. However, I find I still have my custom variables defined in the .zshrc file.

值得注意的是,最新版本的 VSCode 会在检测到您使用 conda 环境时提示您,并建议进行此更改.

It is worth noting that recent versions of VSCode will prompt you when it detects you are using a conda environment, and suggests making this change.

这篇关于路径未从 VSCode 终端中的 conda activate 正确更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:56