为什么macOS Visual Studio代码使用错误的Python解释器? [英] Why Is macOS Visual Studio Code Using Wrong Python Interpreter?
问题描述
我在macOS Sierra(10.12.6)上运行VS Code 1.33.1.当我运行如下所示的简单Python程序时,VS Code使用安装在Mac上的Python 2.7,而不是使用Homebrew安装的Python 3.
I'm running VS Code 1.33.1 on macOS Sierra (10.12.6). When I run a simple Python program like the following, VS Code uses Python 2.7 as installed on my Mac rather than Python 3 which I installed using Homebrew.
# show-python-version.py
import sys
print(sys.version)
以下是"VS代码输出"窗口中显示的输出:
Here's the output as displayed in the VS Code Output window:
[Running] python -u "/Users/smith/Documents/Programming/Python/Examples/show-python-version.py"
2.7.15 (default, May 1 2018, 16:44:37)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
[Done] exited with code=0 in 0.032 seconds
如您所见,我得到的版本是2.7.15,因为它调用的是"python"而不是"python3",但是我不知道如何使用python3.这是我为解决此问题所做的所有事情:
As you can see, I'm getting version 2.7.15 because it's calling "python" instead of "python3", but I don't know how to get it to use python3. Here are all the things I've done to try to fix this problem:
-
在发出代码"时,我确保使用Python 2.7的虚拟环境都未在运行.示例目录中的命令.
I've ensure that none of my virtual environments that use Python 2.7 are running when I issue the "code ." command in the Examples directory.
我使用Homebrew安装了Python 3,因此打开了命令面板(shift + cmd + p),键入"Python:Select Interpreter",并确认它正在使用我的Homebrew版本:current:/usr/local/bin/python3.
I installed Python 3 using Homebrew so I opened the Command Pallette (shift + cmd + p), typed in "Python: Select Interpreter", and confirmed that it's using my Homebrew version: current: /usr/local/bin/python3.
我检查了VS Code的设置,搜索了"python.pythonPath",并确认它与上面第2步中显示的解释器路径相同.
I checked VS Code's Settings, searched for "python.pythonPath" and confirmed that it's the same as the interpreter path shown in step 2 above.
我还检查了Examples/.vscode/settings.json文件,以确认那里的解释器路径:
I also examined the file Examples/.vscode/settings.json to confirm the interpreter path there too:
{
"python.pythonPath": "/usr/local/bin/python3"
}
我已重新启动VS Code,但无济于事.
I've restarted VS Code to no avail.
我已安装以下扩展程序:
I have these extensions installed:
esbenp.prettier-vscode@1.8.1
formulahendry.code-runner@0.9.8
fosshaas.fontsize-shortcuts@1.5.0
ms-python.python@2019.3.6558
tht13.python@0.2.3
vmsynkov.colonize@2.2.2
vscodevim.vim@1.4.0
这可能与我的路径有关,因为python和python3都在/usr/local/bin中.
It's may be related to my path which looks like the following since both python and python3 are in /usr/local/bin:
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:....
我在做什么错了?
推荐答案
这是因为代码运行程序扩展.将此"code-runner.executorMap.python": "python3 -u"
添加到您的settings.json中,这将在运行代码到python3而不是python2时更改python版本
It's because of the code runner extension. Add this "code-runner.executorMap.python": "python3 -u"
to your settings.json and that should change the python version for when it runs code to python3 instead of python2
这篇关于为什么macOS Visual Studio代码使用错误的Python解释器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!