为什么macOS Visual Studio代码使用错误的Python解释器?

问题描述 投票:0回答:1

我在macOS Sierra(10.12.6)上运行VS Code 1.33.1。当我运行如下的简单Python程序时,VS Code使用安装在我的Mac上的Python 2.7,而不是使用Homebrew安装的Python 3。

    # show-python-version.py
    import sys
    print(sys.version)

这是VS Code Output窗口中显示的输出:

    [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。以下是我为解决此问题所做的所有事情:

  1. 我确保在发出“代码”时,我的所有使用Python 2.7的虚拟环境都没有运行。 examples目录中的命令。
  2. 我使用Homebrew安装了Python 3,所以我打开了Command Pallette(shift + cmd + p),输入“Python:Select Interpreter”,并确认它使用的是我的Homebrew版本:current:/ usr / local / bin / python3。
  3. 我检查了VS Code的设置,搜索了“python.pythonPath”并确认它与上面步骤2中显示的解释器路径相同。
  4. 我还检查了文件Examples / .vscode / settings.json以确认那里的解释器路径: { "python.pythonPath": "/usr/local/bin/python3" }
  5. 我重新启动VS Code无济于事。

我安装了这些扩展程序:

[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

它可能与我的路径有关,如下所示,因为python和python3都在/ usr / local / bin中:

    $ echo $PATH
    /usr/local/bin:/usr/bin:/bin:/usr/sbin:....

我究竟做错了什么?

python python-3.x macos visual-studio-code
1个回答
1
投票

这是因为代码运行器扩展将这个"code-runner.executorMap.python": "python3 -u"添加到您的settings.json,这应该更改python版本,因为它运行代码到python3而不是python2

© www.soinside.com 2019 - 2024. All rights reserved.