简体   繁体   English

VSCode 运行 Python 2 而不是 3

[英]VSCode running Python 2 instead of 3

I am using Python with VSCode, and if I use Cmd+Shift+P and type Run Code, it runs the code with Python2 even though I have specified it to use Python3.我在 VSCode 中使用 Python,如果我使用 Cmd+Shift+P 并键入 Run Code,它会使用 Python2 运行代码,即使我已将其指定为使用 Python3。 I have read this tutorial: How to force VSCode to use Python 3 instead of Python 2?我已阅读本教程: 如何强制 VSCode 使用 Python 3 而不是 Python 2? but I do not understand how to change it.但我不明白如何改变它。 (and I can't comment because I don't have enough reputation yay) Could anyone help? (我无法发表评论,因为我没有足够的声誉)有人可以帮忙吗? Thanks!谢谢!

Maybe it could be related to the fact that the mini terminal at the bottom runs "python -u " instead of "python3 -u "?也许这可能与底部的迷你终端运行“python -u”而不是“python3 -u”有关? Does anyone know how to change that?有谁知道如何改变它?

Please note the command Run Code is not provided by the Python Extension for VS Code, instead it is provided by the extension Code Runner .请注意命令Run Code不是由 Python Extension for VS Code 提供,而是由扩展Code Runner 提供 They are two separate extensions.它们是两个独立的扩展。 You'll need to configure that extension to point to the Python Interpter you have chosen.您需要将该扩展配置为指向您选择的 Python Interpter。 Ie as per their docs on the home page you have to update the settings:即根据他们在主页上的文档,您必须更新设置:

{
    "code-runner.executorMap": {
        "python": "<fully qualified path>",
    }
}

Or you have another solution, that's to use the command Python: Run Python file in Terminal .或者您有另一种解决方案,即使用命令Python: Run Python file in Terminal This does not rely on the Code Runner extension and is part of the Python Extension hence using the interpreter you have selected.这不依赖于Code Runner扩展并且是Python Extension一部分,因此使用您选择的解释器。

Try changing the selected Python environment.尝试更改选定的 Python 环境。

选择-python-解释器

This can be accessed by这可以通过访问

  1. Clicking on the Python label at the lower-left of the window单击窗口左下角的 Python 标签
  2. Doing Cmd + Shift + P (Mac) OR Ctrl + Shift + P (Windows/Linux)执行Cmd + Shift + P (Mac) 或Ctrl + Shift + P (Windows/Linux)
  3. Selecting Select Interpreter选择选择解释器

Select Python3 instead of Python2 from the dropdown.从下拉列表中选择 Python3 而不是 Python2。

More info here:更多信息在这里:
https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment

An "environment" in Python is the context in which a Python program runs. Python 中的“环境”是 Python 程序运行的上下文。

Selecting an interpreter from the list adds an entry for python.pythonPath with the path to the interpreter inside your Workspace Settings .从列表中选择一个解释器为python.pythonPath添加一个条目,其中包含Workspace Settings 中解释器的路径。 Because the path is part of the workspace settings, the same environment should already be selected whenever you open that workspace.由于路径是工作区设置的一部分,因此无论何时打开该工作区,都应该已经选择了相同的环境。

The Python extension uses the selected environment for running Python code (using the Python: Run Python File in Terminal command), providing language services (auto-complete, syntax checking, linting, formatting, etc.) when you have a .py file open in the editor, and opening a terminal with the Terminal: Create New Integrated Terminal command. Python 扩展使用选定的环境来运行 Python 代码(使用Python:在终端命令中运行 Python 文件),在打开.py文件时提供语言服务(自动完成、语法检查、linting、格式化等)在编辑器中,然后使用Terminal: Create New Integrated Terminal命令打开一个终端。 In the latter case, VS Code automatically activated the selected environment.在后一种情况下,VS Code 会自动激活所选环境。

As @Don mentioned in his answer the Run Code command is provided by the Code Runner extension not by the Python one.正如@Don 在他的回答中提到的, Run Code命令是由Code Runner扩展提供的,而不是由Python扩展提供的。
Anyway, You can simply add the following to your settings.json file:无论如何,您只需将以下内容添加到settings.json文件中:

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

PS: You need to reload your vs code after doing this so simply hit Ctrl + Shift + P and run Reload Window . PS:执行此操作后,您需要重新加载 vs 代码,因此只需Ctrl + Shift + P并运行Reload Window

don is right 唐是对的

worked for me like a charm 像魅力一样为我工作

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

Did you check your system enviroment variables?你检查你的系统环境变量了吗? Maybe Pyhton 2 is in your PATH instear of Python 3. You should consider uninstalling Python 2 from your PC if It's not going to be missed... I was having a similar trouble working with Machine Learn and it solved for me.也许 Pyhton 2 在 Python 3 的 PATH 中。如果不会错过它,您应该考虑从 PC 上卸载 Python 2...我在使用机器学习时遇到了类似的问题,它为我解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM