如何设置 Visual Studio 代码以在 Windows 10 中查找 Python 3 解释器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43313903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:51:26  来源:igfitidea点击:

How to setup Visual Studio code to find Python 3 interpreter in Windows 10

pythonvisual-studio-code

提问by Varuna

In Windows 10 how to set up Visual Studio code to find the Python 3 interpreter. In Windows 10 Visual Studio code is not finding the Python 3 interpreter. I have added the Python extension which can be found at https://marketplace.visualstudio.com/items?itemName=donjayamanne.python. How to edit the settings.js to use Python?Even if the python.pythonPath in settings.js is changed to lead to the python.exe, which in windows 10 is "C:\Users\\AppData\Local\Programs\Python\Python36-32\p?ython.exe". VS code, when entered Python: Select Workspace Interpreterinto the command palette, displays the message "Please open a workspace to select the Python Interpreter".Why?

在 Windows 10 中如何设置 Visual Studio 代码以查找 Python 3 解释器。在 Windows 10 中,Visual Studio 代码找不到 Python 3 解释器。我添加了可以在https://marketplace.visualstudio.com/items?itemName=donjayamanne.python找到的 Python 扩展。如何编辑 settings.js 以使用 Python?即使将 settings.js 中的 python.pythonPath 更改为导致 python.exe,在 Windows 10 中为“C:\Users\\AppData\Local\Programs\Python \Python36-32\p?ython.exe”。VS 代码在进入Python: Select Workspace Interpreter命令面板时显示消息“ Please open a workspace to select the Python Interpreter”。为什么?

回答by Edison

macOS

苹果系统

VSCode -> Preferences -> Settings -> scroll down to Python Configuration.

VSCode -> 首选项 -> 设置 -> 向下滚动到 Python 配置。

Click the pencil on the left of the configuration you'd like to use and click Copy Settings.

单击您要使用的配置左侧的铅笔,然后单击复制设置。

"python.pythonPath": "python3.6"

"python.pythonPath": "python3.6"

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Cabara

Go to File/Preferences/Settingsand find Python Configuration. This is where you manually change the configurations.

File/Preferences/Settings寻找Python Configuration。这是您手动更改配置的地方。

You can also select your interpreter from command palette. The following instructions are from DonJayamanne's Github.

您还可以从 中选择您的口译员command palette。以下说明来自DonJayamanne 的 Github

Select the command Python: Select Workspace Interpreterfrom the command palette

Upon selecting the above command a list of discovered interpreters will be displayed in a quick pick list

Selecting an interpreter from this list will update the settings.jsonfile automatically.

Python: Select Workspace Interpreter命令面板中选择命令

选择上述命令后,将在快速选择列表中显示发现的解释器列表

从此列表中选择一个解释器将settings.json自动更新文件。

Command Pallet Screenshot

命令托盘截图

回答by thanhtang

In the current directory where you open you VS Code, go to .vscode/settings.jsonand modify this line in the json file:

在打开 VS Code 的当前目录中,转到.vscode/settings.json并修改 json 文件中的这一行:

 {
    "python.pythonPath": "path_to_your_python_bin", 
    # e.g., /home/myname/venv/bin/python3.7 or to a .exe file if you are on Windows
 }

回答by jps

I'm also using this Python extensionin Visual Studio Code on Windows 10. Python is installed in C:\Python27\ and C:\Python36\ and both folders are added to the Windows PATH variableWith this setting VS Code should be able to find the Python interpreter. At least in my environment it worked well.

我也在Windows 10 上的 Visual Studio Code 中使用此 Python 扩展。Python 安装在 C:\Python27\ 和 C:\Python36\ 中,并且两个文件夹都添加到Windows PATH 变量中使用此设置 VS Code 应该能够找到 Python 解释器。至少在我的环境中它运行良好。

But VS takes the first interpreter it can find, which was in my case the Version 3.6. When I need 2.7 for a project I have to tell VS Code explicitely to use that one in the configration file settings.jsonIn VS Code press Ctrl,and then, in the right window add:

但是 VS 使用它可以找到的第一个解释器,在我的情况下是 3.6 版。当我需要 2.7 用于项目时,我必须明确地告诉 VS Code 在配置文件中使用那个settings.json在 VS Code 中按下Ctrl,,然后在右侧窗口中添加:

{
"python.pythonPath": "c:/python27/python.exe"
}

or whatever is the path to the Python interpreter on your PC.

或者你电脑上的 Python 解释器的路径是什么。

This is also described in the Wiki under Manual Configuration. Now, with this setting Python 2.7 was used.

这也在Wiki 的 Manual Configuration 下进行了描述。现在,使用此设置使用 Python 2.7。

Something that did not work was that the automatic detection of the Python versions and therefore I could also not choose the version I need with the Select an Interpretercommand.

不起作用的是 Python 版本的自动检测,因此我也无法使用Select an Interpreter命令选择我需要的版本。

回答by HeyJude

I also had this problem. I did what @jpssaid and it still didn't work, but then I changed my Environment Settings order so that Python36 precedes Python27, and it worked.

我也有这个问题。我做了@jps所说的,它仍然没有用,但后来我改变了我的环境设置顺序,使 Python36 在 Python27 之前,并且它起作用了。

回答by Eric Aldinger

In my case I have seen the actual workspace you are working in override the File/Preferences/Settings (Windows) or Code/Preferences/Settings (Mac).

就我而言,我已经看到您正在使用的实际工作区覆盖了文件/首选项/设置 (Windows) 或代码/首选项/设置 (Mac)。

The solution was to manually edit the workspace file (usually in your home directory) or to create a new workspace and re-add your directory for your code.

解决方案是手动编辑工作区文件(通常在您的主目录中)或创建一个新的工作区并为您的代码重新添加您的目录。

回答by monketrip28

Uninstall x64 version. and Install the x86 32bit version. Exclude python from AV. this should do the trick. The Python extension. Be Sure to restart the VSCode each time.

卸载 x64 版本。并安装 x86 32 位版本。从 AV 中排除 python。这应该可以解决问题。Python 扩展。确保每次都重新启动 VSCode。