如何在 Visual Studio Code 中更改 python 版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48135624/
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
How can I change python version in Visual Studio Code?
提问by Paige
There is a problem I can't solve. I already set up :
有一个我无法解决的问题。我已经设置了:
1.user setting
1.用户设置
{
"atomKeymap.promptV3Features": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.formatOnPaste": true,
"python.pythonPath": "python3",
"command": "python3",
}
}
2.Workspace Settings
2.工作区设置
{
"python.pythonPath": "${workspaceFolder}/env/bin/python3.6",
"git.ignoreLimitWarning": true
}
}
3.tasks.jason file
3.tasks.jason文件
{
// 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 debug console and the version and path is right, debug
如果我使用调试控制台并且版本和路径正确,则 调试
But my output always default "python2.7" no matter what I do. How can I improve it? output
但无论我做什么,我的输出始终默认为“python2.7”。我该如何改进? 输出
回答by jmh
Under the view menu select 'show command pallet'. One of the commands you can then select from the command palette is 'Python: Select Interpreter'. Selecting this option will allow you to choose which version of python to use.
在视图菜单下选择“显示命令托盘”。然后您可以从命令面板中选择的命令之一是“Python:选择解释器”。选择此选项将允许您选择要使用的 Python 版本。
回答by Rustam A.
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. 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:
(首先尝试更新#1)查看您的屏幕截图,我看到您正在使用 Code Runner 扩展。我想这就是您启动程序的方式。我不知道它内部是如何工作的,但是将这一行添加到 Code-Runner 扩展 setting.json 文件中修复了它:
"code-runner.executorMap.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. This became not so convenient further - I started using python's virual environmentsand the solution above couldn't launch these environments, cause python3 (symlink) is always linking to the same python environment and only python symlink gets changed by environment management packages.
更新#1。这变得不那么方便了 - 我开始使用 python 的虚拟环境,上面的解决方案无法启动这些环境,因为 python3(符号链接)总是链接到同一个 python 环境,只有 python 符号链接被环境管理包更改。
The problem here is Code-Runner extension doesn't use VSCode's python.PythonPath(Workplace) variable - the one that is changed when you "change python interpreter". Disable Code-Runner and VSCode will work more or less in a predictable manner. Thought it will forget to deactivate environments when you choose another interpreter.
这里的问题是 Code-Runner 扩展不使用 VSCode 的 python.PythonPath(Workplace) 变量 - 当您“更改 python 解释器”时更改的变量。禁用 Code-Runner 和 VSCode 将或多或少以可预测的方式工作。认为当您选择另一个解释器时它会忘记停用环境。
回答by Muhannad
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.
On the right side click on the ... (the three dots) and select (open settings.json)
In the search bar type 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.
Look for "python" and change the command next to it to "python3".
Save the changes and you should be good to go.
在 Vscode 中,单击文件 > 首选项 > 设置。
在右侧单击 ...(三个点)并选择(打开 settings.json)
在搜索栏中输入 code-runner.executorMap。
您只能更改右侧的设置。
在最后一个设置后输入一个逗号,然后输入“code-runner.executorMap”并按回车键,这将复制默认文件中的所有设置。
查找“python”并将其旁边的命令更改为“python3”。
保存更改,您应该就可以开始了。
回答by Mindbulletz
Tot's answer is what worked for me on windows 10, with a few modifications.
Tot 的答案是在 Windows 10 上对我有用的方法,经过一些修改。
- File -> Preferences -> Settings
- Type in "python.pythonPath" in the search bar.
- Change it to what you normally run python with from the command line. If you have your Path environment variable set, that's just
python
. If not, it's likely the full path to the executable.
- 文件 -> 首选项 -> 设置
- 在搜索栏中输入“python.pythonPath”。
- 将其更改为您通常从命令行运行 python 的内容。如果您设置了 Path 环境变量,那只是
python
. 如果不是,则可能是可执行文件的完整路径。
回答by EngineSense
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
select the ... on the right corner side. You'll see USER SETTINGS, WORKSPACE SETTINGS, YOURAPP_NAME_SETTINGS. click on the your_app_name.
"python.pythonPath": "/home/Jhon/AllWorksUbuntu/Projects/VX-350/envname/bin/python"
VSCODE主页-->文件-->首选项-->设置
选择右上角的 ...。您会看到“用户设置”、“工作区设置”、“YOURAPP_NAME_SETTINGS”。单击 your_app_name。
"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!!!
在上面播放以设置正确的路径。你可以走了!!!
回答by MEB
Just a preface: VS code was working fine (Using Python 3.x) and seemingly out of the blue it started using Python 2.7. The input() function would not convert the input to a string and that's when I realized what was happening. Typing Python in the terminal window showed 2.7 was running.
只是一个前言:VS 代码运行良好(使用 Python 3.x)并且似乎突然开始使用 Python 2.7。input() 函数不会将输入转换为字符串,那时我才意识到发生了什么。在终端窗口中输入 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. I got a message from VS Code (it was running) that 3.8 got installed. I clicked Terminal/New Terminal, typed Python and it showed version 3.8. Typed 'Python xxx.py' and the code started working as it had been.
我删除了环境变量并从 Microsoft Store 重新安装了 Python 3.8,它立即安装了。我从 VS Code(它正在运行)收到一条消息,表明 3.8 已安装。我点击终端/新终端,输入 Python 并显示版本 3.8。输入“Python xxx.py”,代码开始正常工作。