VSCode 终端显示不正确的 python 版本和路径,从 anaconda 启动终端工作正常

问题描述

我已经被这个问题困扰了好几个小时,并且相信我已经尝试了所有方法,除了把我的电脑扔出窗外.

I have been stuck on this one problem for hours now and believe I have tried everything outside of throwing my computer out of the window.

我使用 python 3.7 版和 Django 2.1 版在 Anaconda 上设置了一个虚拟环境.如果我从 Anaconda 激活这个虚拟环境,一切都会顺利进行.

I have a virtual environment set up on Anaconda using python version 3.7 and Django version 2.1. If I activate this virtual environment from Anaconda everything works smoothly.

(movierecommender) bash-3.2$ python -V
Python 3.7.2
(movierecommender) bash-3.2$ python -m django --version
2.1.5

但是,当我尝试从 vscode 终端激活环境时,我得到了

However when I try to activate the environment from a vscode terminal I get

(movierecommender) maxs-MBP:movies maxswann$ python -V
Python 2.7.10
(movierecommender) maxs-MBP:movies maxswann$ python -m django --version
/usr/bin/python: No module named django

我有 Python 3.7.2 64 位 ('movierecommender':conda)在我的 vscode 窗口左下角显示为我的 python 解释器,但仍然得到错误的 python 版本

I have Python 3.7.2 64-bit ('movierecommender':conda) showing as my python interpreter in the bottom left of my vscode window yet still get the wrong python version

我认为这可能与 PYTHONPATH 有关,但尝试取消设置和重置,即使我不必担心 Anaconda 中的这一点,因为它会自动添加:"python.pythonPath":"/Users/maxswann/anaconda3/envs/movierecommender/bin/python"到 settings.json.vscode 文件

I thought this may be to do with the PYTHONPATH but have tried unsetting and resetting even though I should not have to worry about this in Anaconda as it automatically adds: "python.pythonPath":"/Users/maxswann/anaconda3/envs/movierecommender/bin/python" to a settings.json.vscode file

使用:

python -c "import sys; print(sys.path)"

Anaconda 推出的终端

Anaconda-launched terminal

['', '/Users/maxswann/anaconda3/envs/movierecommender/lib/python37.zip', '/Users/maxswann/anaconda3/envs/movierecommender/lib/python3.7', '/Users/maxswann/anaconda3/envs/movierecommender/lib/python3.7/lib-dynload', '/Users/maxswann/anaconda3/envs/movierecommender/lib/python3.7/site-packages']

VS代码终端

['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

如您所见,它似乎使用的是默认的 mac OS python 版本.

As you can see it seems to be using the default mac OS python version.

以前有没有其他人遇到过这个问题?我整天都在扯头发试图解决这个问题

Has anybody else had this problem before? I've been tearing hair out all day trying to fix this


解决方案

我也遇到了同样的问题.终于从论坛(https://github.com/Microsoft/vscode-python/issues/4434#issuecomment-466600591)

I have been facing the exact same problem. Finally found a workaround from a forum (https://github.com/Microsoft/vscode-python/issues/4434#issuecomment-466600591)

只要在配置中添加一些东西,terminal.integrated.env.osx,在shell初始化(source bash_profile or zshrc)后,内容会附加到PATH).在我的 Mojave 中,我只需将以下空条目添加到我的用户配置中:

As long as you ADD some stuff to configuration, terminal.integrated.env.osx, the content will be appended to PATH after shell initialization(source bash_profile or zshrc). In my Mojave, I simply add following empty entry to my user configuration:

"terminal.integrated.env.osx": {
        "PATH": ""
}

那么$PATH就和外部终端一样了.

Then the $PATH will be the same as the external terminal.

相关文章