【问题标题】:Using Python3 on Mac OS X With Visual Studio Code - not working在 Mac OS X 上使用 Python3 和 Visual Studio Code - 不工作
【发布时间】:2017-11-05 19:18:06
【问题描述】:

在调试它继续使用 Python2 时,我似乎无法让 Python3 工作。我已经尝试了一些东西,但仍然无法让它工作。我验证了 Python3s 路径是 /usr/local/bin/python3 每次我运行一个脚本到 by

import sys
print(sys.version)

我仍然得到 2.7.10 python 3 已安装并正常工作。

我的 launch.json 和 settings.json 中有这个(类型和请求在 launch.json 中由于某种原因都是灰色的)

 {
"name": "Python",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"pythonPath": "/usr/local/bin/python3",
"debugOptions": [
    "WaitOnAbnormalExit",
    "WaitOnNormalExit",
    "RedirectOutput"
] }

【问题讨论】:

  • 我对 Visual Studio Code 不是很熟悉,但 pythonPath 通常是您的 python lib 文件夹的路径,而不是您的 python 可执行文件的路径?
  • 你是如何在 VS Code 中运行程序的?我有一个调试配置设置为使用我在单独文件夹中的 virtualenv 中的 python 二进制文件。但是,如果我在终端(甚至在代码中)运行 which python,它会将我指向我的 /usr/local/bin/python。

标签: python json macos python-3.x visual-studio-code


【解决方案1】:

这不是答案,但我的代表不允许发表评论: 对我来说,您提出的解决方案效果很好。这里有一些故障排除:

  • 您是否更改了原始的 launch.json 条目?然后你可能需要重新加载它,这应该会自动发生,但重新启动 VSCode 肯定会这样做。
  • 另外,我建议使用“添加配置”按钮,然后选择“python”,并在新条目中编辑名称(从“Python”到“Python3”),以免与旧的 python 条目冲突,以及将“pythonPath”键的值从“${config:python.pythonPath}”更改为“/usr/local/bin/python3”

要使用此配置,请在“调试”下拉框中选择。

【讨论】:

    【解决方案2】:

    如果您使用“代码运行器”,您可以尝试使用命令面板打开用户设置。

    {
        "code-runner.executorMap": {
            "python": "python3"
        }
    }
    

    这对我有用。

    【讨论】:

      【解决方案3】:

      对我有用的是在用户设置中添加以下内容:

      {
          "python.pythonPath": "/usr/local/bin/python3"
      }
      

      【讨论】:

      • python.pythonPath 应该指向你想在 vscode 中使用的 python 解释器的路径来调试和运行 python 脚本。就我而言,它是/usr/local/bin/python3,因为我使用自制软件安装了 python 3。
      【解决方案4】:

      要选择特定的 Python 解释器,请从 Command Palette (⇧⌘P) 调用 Python: Select Interpreter 命令。

      这将更新您的 .vscode/settings.json 文件:

      {
          "python.pythonPath": "/usr/local/bin/python3"
      }
      

      然后您应该能够使用 python 3 解释器或您的机器上可用的任何其他 python 解释器进行调试。

      【讨论】:

      • 问题是python3即使安装到系统中也不会出现在菜单中。
      • 如果您在系统中安装了 python3 但它没有显示,可能意味着它不在您的 PATH 环境变量中。你检查了吗?
      猜你喜欢
      • 2019-01-31
      • 1970-01-01
      • 2015-07-09
      • 2020-07-16
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 2019-07-27
      • 2016-03-09
      相关资源
      最近更新 更多