[英]How can I change the Python version in Visual Studio Code?
These are my settings:这些是我的设置:
User Settings用户设置
{ "atomKeymap.promptV3Features": true, "editor.multiCursorModifier": "ctrlCmd", "editor.formatOnPaste": true, "python.pythonPath": "python3", "command": "python3", }
Workspace Settings工作区设置
{ "python.pythonPath": "${workspaceFolder}/env/bin/python3.6", "git.ignoreLimitWarning": true }
tasks.json任务.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "0.1.0", "command": "python3", "isShellCommand": true, "args": ["${file}"], "showOutput": "always" }
If I use the debug console, the version and path is right:如果我使用调试控制台,版本和路径是正确的:
But the output always defaults to "python2.7", no matter what I do.但无论我做什么,输出总是默认为“python2.7”。
How can I fix this?我怎样才能解决这个问题?
Under the view menu select 'show command pallet'.在视图菜单下选择“显示命令托盘”。 One of the commands you can then select from the command palette is 'Python: Select Interpreter'.
然后您可以从命令面板中选择的命令之一是“Python:选择解释器”。 Selecting this option will allow you to choose which version of python to use.
选择此选项将允许您选择要使用的 python 版本。
UPD .升级版。 First, read the update #1 part in the bottom
首先,阅读底部的更新#1部分
(Try update#1 first) Looking at your screenshots I see you are using Code Runner extension. (首先尝试更新#1)查看您的屏幕截图,我看到您正在使用 Code Runner 扩展。 And I guess that is the way you are launching your programs.
我想这就是你启动程序的方式。 I don't know how it works internally, but adding this line into Code-Runner extension setting.json file fixed it:
我不知道它在内部是如何工作的,但是将此行添加到 Code-Runner 扩展 setting.json 文件中修复了它:
"code-runner.executorMap.python": {...
"python": "python3 -u",
...}
Found it in Code-Runner GitHub repository: https://github.com/formulahendry/vscode-code-runner/issues/366在 Code-Runner GitHub 存储库中找到它: https ://github.com/formulahendry/vscode-code-runner/issues/366
If you type "python --help", you'll see "-u" flag stands for "unbuffered binary stdout and stderr..." - don't know why it matters here.如果你输入“python --help”,你会看到“-u”标志代表“无缓冲的二进制标准输出和标准错误......”——不知道为什么它在这里很重要。
Update #1 .更新 #1 。 This became not so convenient further - I started using python's virual environments and the solution above couldn't launch these environments, cause python3 (symlink) is always linking to the same python environment.
这变得不那么方便了——我开始使用 python 的虚拟环境,上面的解决方案无法启动这些环境,因为 python3(符号链接)总是链接到同一个 python 环境。
The solution here is to use Code-Runner's supported customized parameters, so you should change "python" line in it's settings.json to:这里的解决方案是使用 Code-Runner 支持的自定义参数,因此您应该将其 settings.json 中的“python”行更改为:
...
"python": "$pythonPath $fullFileName",
...
Tot's answer is what worked for me on windows 10, with a few modifications. Tot 的答案是在 Windows 10 上对我有用的,并进行了一些修改。
python
.python
。 If not, it's likely the full path to the executable.This solution is for Mac and Linux:此解决方案适用于 Mac 和 Linux:
To change your Python version from 2.7 to 3 do this:要将 Python 版本从 2.7 更改为 3,请执行以下操作:
In Vscode click on file > preferences > settings.在 Vscode 中单击文件 > 首选项 > 设置。
On the right side click on the ... (the three dots) and select (open settings.json)在右侧单击 ...(三个点)并选择(打开 settings.json)
In the search bar type code-runner.executorMap.在搜索栏中输入 code-runner.executorMap。
You can only change the settings on the right side.您只能更改右侧的设置。
After the last setting type a comma then "code-runner.executorMap" and hit enter, this will copy all the settings from the default file.在最后一个设置后输入一个逗号,然后输入“code-runner.executorMap”并回车,这将复制默认文件中的所有设置。
Look for "python" and change the command next to it to "python3".查找“python”并将其旁边的命令更改为“python3”。
Save the changes and you should be good to go.保存更改,您应该一切顺利。
By default, it doesn't know about your interpreter, and will initialize using the default .bashrc
or equivalent in the OS.默认情况下,它不知道您的解释器,并将使用默认的.bashrc
或操作系统中的等效项进行初始化。
I found two relevant settings from an issue in Feb 2021 .我从 2021 年 2 月的一个问题中找到了两个相关设置。 Checking the second option
Python > Terminal: Activate Environment
enables automatic activation of virtual environment:检查第二个选项
Python > Terminal: Activate Environment
启用虚拟环境的自动激活:
In settings.json
it is called "python.terminal.activateEnvironment": true
.在
settings.json
,它被称为"python.terminal.activateEnvironment": true
。
Update (8 Aug 2021): Today when I opened the terminal from VS Code on Windows, it automatically inserted a line of code & C:/Users/[UserName]/[venv]/Scripts/Activate.ps1
to activate the appropriate environment associated with the selected python interpreter!更新(2021 年 8 月 8 日):今天当我在 Windows 上从 VS Code 打开终端时,它会自动插入一行代码
& C:/Users/[UserName]/[venv]/Scripts/Activate.ps1
以激活相应的环境与选定的python解释器相关联! It appears the aforementioned settings is now the default behavior.看来上述设置现在是默认行为。 While there are changes to Terminal behavior in the release notes of July 2021 (version 1.59) , I don't see virtual environment activation being explicitly mentioned.
虽然2021 年 7 月的发行说明(版本 1.59)中对终端行为进行了更改,但我没有看到明确提及虚拟环境激活。
The new behavior is documented here, in " Environments and Terminal windows ".新行为记录在此处的“ 环境和终端窗口”中。
"python.pythonPath"
has been deprecated "python.pythonPath"
已被弃用Therefore most previous answers are outdated.因此,大多数以前的答案都已过时。 Use
"python.defaultInterpreterPath"
instead:改用
"python.defaultInterpreterPath"
:
2021.6.0 (16 June 2021)
2021.6.0(2021 年 6 月 16 日)
5. Added python.defaultInterpreterPath setting at workspace level when in pythonDeprecatePythonPath experiment.5. 在pythonDeprecatePythonPath 实验中添加了工作区级别的python.defaultInterpreterPath 设置。 (#16485)
(#16485)
8. Show python.pythonPath deprecation prompt when in pythonDeprecatePythonPath experiment.8、在pythonDeprecatePythonPath实验中显示python.pythonPath弃用提示。 (#16485)
(#16485)
2020.7.0 (16 July 2020)
2020.7.0(2020 年 7 月 16 日)
9. Prompt users that we have deleted pythonPath from their workspace settings when in Deprecate PythonPath experiment.9. 在 Deprecate PythonPath 实验中,提示用户我们已从他们的工作区设置中删除了 pythonPath。 (#12533)
(#12533)
2020.5.0 (12 May 2020)
2020.5.0(2020 年 5 月 12 日)
6. Do a one-off transfer of existing values for python.pythonPath setting to new Interpreter storage if in DeprecatePythonPath experiment.6. 如果在 DeprecatePythonPath 实验中,将 python.pythonPath 设置的现有值一次性传输到新的解释器存储。 (#11052)
(#11052)
8. Added prompt asking users to delete python.pythonPath key from their workspace settings when in Deprecate PythonPath experiment.8. 在弃用 PythonPath 实验中,增加了提示用户从其工作区设置中删除 python.pythonPath 键的提示。 (#11108)
(#11108)
12. Rename string ${config:python.pythonPath} which is used in launch.json to refer to interpreter path set in settings, to ${config:python.interpreterPath}.12. 将在launch.json 中用于引用设置中设置的解释器路径的字符串${config:python.pythonPath} 重命名为${config:python.interpreterPath}。 (#11446)
(#11446)
2020.4.0 (20 April 2020)
2020.4.0(2020 年 4 月 20 日)
13. Added a user setting python.defaultInterpreterPath to set up the default interpreter path when in Deprecate PythonPath experiment.13.新增用户设置python.defaultInterpreterPath,用于设置Deprecate PythonPath实验时的默认解释器路径。 (#11021)
(#11021)
If you wish to set a default python interpreter for all workspaces, open settings with Ctrl+Shift+P
, Preferences: Open User Settings
and search for Python: Default Interpreter Path
.如果您希望为所有工作区设置默认 python 解释器,请使用
Ctrl+Shift+P
打开设置, Preferences: Open User Settings
并搜索Python: Default Interpreter Path
。 Otherwise, if you want to set it for only the current workspace, use Preferences: Open Workspace Settings
instead.否则,如果您只想为当前工作区设置它,请改用
Preferences: Open Workspace Settings
。
In your case, you wish to set it to ${workspaceFolder}/env/bin/python3.6
.在您的情况下,您希望将其设置为
${workspaceFolder}/env/bin/python3.6
。 If you edit settings.json
directly instead of using the GUI:如果您直接编辑
settings.json
而不是使用 GUI:
{
"python.defaultInterpreterPath": "${workspaceFolder}/env/bin/python3.6"
}
Detailed instructions can be found in the documentation "Manually specify an interpreter" , including using environment variables as the interpreter's path.详细说明可以在“手动指定解释器”文档中找到,包括使用环境变量作为解释器的路径。
In VSCode there are two paths of python:在 VSCode 中有两条 python 路径:
For sanity purposes you should make sure that both "Python: Select Interpreter" and the system environment variables point to the same version of Python.出于理智的目的,您应该确保“Python:选择解释器”和系统环境变量都指向相同版本的 Python。
Bonus goodie in Windows 10. If you don't have environment variable setup, and you type 'python' in VSCode terminal, it'll point to C:\Users\YourName\AppData\Local\Microsoft\WindowsApps\python.exe, which just opens up python link in Windows AppStore 🙄. Windows 10 中的额外好处。如果您没有设置环境变量,并且在 VSCode 终端中键入“python”,它将指向 C:\Users\YourName\AppData\Local\Microsoft\WindowsApps\python.exe,它只是在 Windows AppStore 中打开 python 链接🙄。
Several of the answers here explain good approaches, but below are my top 2 recommendations.这里的几个答案解释了好的方法,但以下是我的前 2 条建议。
I find this the quickest approach;我发现这是最快的方法; however, it isn't always available for first-time users.
但是,它并不总是可供初次使用的用户使用。 If you're already using Python in VS Code, this is usually the easiest way to reach the Python: Select Interpreter menu.
如果您已经在 VS Code 中使用 Python,这通常是访问Python 的最简单方法:选择解释器菜单。 On the bottom left of your screen, look for "Python XXX ".
在屏幕的左下方,查找“Python XXX ”。 This is the currently detected/configured version of Python for your project, and clicking it brings you to the interpreter menu to change the Python version you're using.
这是当前为您的项目检测/配置的 Python 版本,单击它会将您带到解释器菜单以更改您正在使用的 Python 版本。 At the time of writing, I was using Python 3.9.1 as seen in the snippet below:
在撰写本文时,我使用的是 Python 3.9.1,如下面的片段所示:
As @jmh denoted in his answer, you can also use the 'View' tab to navigate to the Command Palette .正如@jmh在他的回答中指出的那样,您还可以使用“查看”选项卡导航到Command Palette 。 In the Command Palette, search for Python: Select Interpreter to bring about the same menu denoted above.
在 Command Palette 中,搜索Python:选择 Interpreter以显示与上述相同的菜单。
Happy coding!快乐编码!
Late answer really, if you find difficult to set the python version in VsCode,真的迟到的答案,如果你发现很难在 VsCode 中设置 python 版本,
If the interpreter didn't show the envname/bin/python or any desired path you want, then go to如果解释器没有显示 envname/bin/python 或您想要的任何所需路径,请转到
VSCODE main page -->file-->preference-->settings VSCODE 主页-->文件-->首选项-->设置
select the ... on the right corner side.选择右上角的 ...。 You'll see USER SETTINGS, WORKSPACE SETTINGS, YOURAPP_NAME_SETTINGS.
您会看到 USER SETTINGS、WORKSPACE SETTINGS、YOURAPP_NAME_SETTINGS。 click on the your_app_name.
点击 your_app_name。
"python.pythonPath": "/home/Jhon/AllWorksUbuntu/Projects/VX-350/envname/bin/python"
“python.pythonPath”:“/home/Jhon/AllWorksUbuntu/Projects/VX-350/envname/bin/python”
Play on the above to set the correct path.在上面播放以设置正确的路径。 You're good to go!!!
你可以走了!!!
Worked for me (linux user);为我工作(linux用户);
Assuming that you have other python versions installed in your system:假设您的系统中安装了其他 python 版本:
Looks like put only 'python' will always bring python 2.看起来只放'python'总是会带来python 2。
Just a preface: VS code was working fine (Using Python 3.x) and seemingly out of the blue it started using Python 2.7.只是一个前言:VS 代码运行良好(使用 Python 3.x),并且似乎出乎意料地开始使用 Python 2.7。 The input() function would not convert the input to a string and that's when I realized what was happening.
input() 函数不会将输入转换为字符串,这时我才意识到发生了什么。 Typing Python in the terminal window showed 2.7 was running.
在终端窗口中键入 Python 显示 2.7 正在运行。
For me....为了我....
Even though "python.pythonPath" was pointing to a seemingly correct location (C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64), one of my environment variables was pointing to C:\Users\Mike.windows-build-tools\python27.即使“python.pythonPath”指向一个看似正确的位置(C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64),我的环境变量之一指向 C:\Users\Mike.windows-构建工具\python27。
I deleted the environment variable and reinstalled Python 3.8 from the Microsoft Store and it immediately installed.我删除了环境变量并从 Microsoft Store 重新安装了 Python 3.8,它立即安装了。 I got a message from VS Code (it was running) that 3.8 got installed.
我从 VS Code(它正在运行)收到一条消息,说安装了 3.8。 I clicked Terminal/New Terminal, typed Python and it showed version 3.8.
我点击了终端/新终端,输入了 Python,它显示了 3.8 版。 Typed 'Python xxx.py' and the code started working as it had been.
输入“Python xxx.py”,代码开始正常工作。
In my case, I checked the python version using就我而言,我使用
python --version
It showed python 2.x even though my interpreter path was 3.x.即使我的解释器路径是 3.x,它也显示了 python 2.x。 So uninstalled python 2.x from my computer through control panel.
所以通过控制面板从我的电脑上卸载了python 2.x。 Then it worked fine for me.
然后它对我来说很好。
One thing that can also be missed is the shell profile you are using in your terminal where you see the wanted python version vs the profile in your VS Code.还可能错过的一件事是您在终端中使用的 shell 配置文件,您可以在其中看到想要的 python 版本与 VS Code 中的配置文件。
>Terminal: Select default profile
>Terminal: Select default profile
安装 Anaconda,然后选择它作为解释器是一种将 Python 设置为最新版本并获得使 Jupyter Notebook 工作的扩展的简单方法。
In Vscode you can go to preferences > settings, then on the right menu click on the first icon which is JSON.在 Vscode 中,您可以转到首选项 > 设置,然后在右侧菜单中单击第一个图标,即 JSON。 Look for "python.pythonPath" and "python.defaultInterpreterPath" and change the path.
查找“python.pythonPath”和“python.defaultInterpreterPath”并更改路径。 To find python3 path, open terminal and execute
python3 dummyname
.要查找 python3 路径,请打开终端并执行
python3 dummyname
。 Actually you'll face an error but the point is it will show you the path!实际上你会遇到一个错误,但关键是它会告诉你路径!
Updating @Isabella answer , using current VSCode 1.65.1 and current python launcher, you can use py -version
, for example py -3.8
.更新@Isabella answer ,使用当前的 VSCode 1.65.1 和当前的 python 启动器,您可以使用
py -version
,例如py -3.8
。
Thus, you can make a folder .vscode
containing a single file name called settings.json
containing因此,您可以创建一个
.vscode
文件夹,其中包含一个名为settings.json
的文件名,其中包含
{
"python.defaultInterpreterPath": "py -3.8",
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.