Pai*_*ige 12 python visual-studio-code
有一个我无法解决的问题。我已经设置好了:
1.用户设置
{
"atomKeymap.promptV3Features": true,
"editor.multiCursorModifier": "ctrlCmd",
"editor.formatOnPaste": true,
"python.pythonPath": "python3",
"command": "python3",
Run Code Online (Sandbox Code Playgroud)
}
2.工作区设置
{
"python.pythonPath": "${workspaceFolder}/env/bin/python3.6",
"git.ignoreLimitWarning": true
Run Code Online (Sandbox Code Playgroud)
}
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"
Run Code Online (Sandbox Code Playgroud)
}
如果我使用调试控制台并且版本和路径正确,请 调试
但是无论我做什么,我的输出始终默认为“ python2.7”。我该如何改善? 输出
jmh*_*jmh 15
在视图菜单下,选择“显示命令托盘”。然后可以从命令选项板中选择的命令之一是“ Python:Select Interpreter”。选择此选项将允许您选择要使用的python版本。
Chi*_*hus 15
这里的几个答案解释了好的方法,但以下是我最重要的两个建议。
1) 底部屏幕导航(易于访问)
2) 命令面板
如果您仍然遇到问题,还有一个 VS Code 入门指南,可引导您设置虚拟环境和/或为支持所需语言的 Python 选择不同的解释器: https: //code.visualstudio.com/docs/ python/python-教程
快乐编码!
小智 11
Tot 的答案是在 Windows 10 上对我有用的方法,经过一些修改。
python. 如果不是,则可能是可执行文件的完整路径。默认情况下,它不知道您的解释器,并将使用.bashrc操作系统中的默认值或等效值进行初始化。
我从 2021 年 2 月的一个问题中找到了两个相关设置。勾选第二个选项Python > Terminal: Activate Environment可以自动激活虚拟环境:
其中settings.json称为"python.terminal.activateEnvironment": true.
更新(2021 年 8 月 8 日):今天,当我在 Windows 上从 VS Code 打开终端时,它自动插入一行代码& C:/Users/[UserName]/[venv]/Scripts/Activate.ps1来激活与所选 python 解释器关联的适当环境!看来上述设置现在是默认行为。虽然2021 年 7 月的发行说明(版本 1.59)中对终端行为进行了更改,但我没有看到明确提及虚拟环境激活。
新行为记录在此处的“环境和终端窗口”中。
"python.pythonPath" 已被弃用因此,以前的大多数答案都已过时。改用"python.defaultInterpreterPath":
2021.6.0(2021年6月16日)
5.在pythonDeprecatePythonPath实验中在工作区级别添加了python.defaultInterpreterPath设置。(#16485)
8. 在 pythonDeprecatePythonPath 实验中显示 python.pythonPath 弃用提示。(#16485)
2020.7.0 (2020年7月16日)
9. 在弃用PythonPath实验时,提示用户我们已从他们的工作空间设置中删除了pythonPath。(#12533)
2020.5.0(2020 年 5 月 12 日)
6. 如果在 DeprecatePythonPath 实验中,请将 python.pythonPath 设置的现有值一次性传输到新的 Interpreter 存储。(#11052)
8. 添加了提示,要求用户在弃用 PythonPath 实验时从其工作区设置中删除 python.pythonPath 键。(#11108)
12. 将 launch.json 中用于引用设置中设置的解释器路径的字符串 ${config:python.pythonPath} 重命名为 ${config:python.interpreterPath}。(#11446)
2020.4.0(2020年4月20日)
13.添加用户设置python.defaultInterpreterPath,用于在弃用PythonPath实验时设置默认解释器路径。(#11021)
如果您希望为所有工作区设置默认的 python 解释器,请使用 打开设置Ctrl+Shift+P,Preferences: Open User Settings然后搜索 Python: Default Interpreter Path. 否则,如果您只想将其设置为当前工作区,请Preferences: Open Workspace Settings改用。
就您而言,您希望将其设置为${workspaceFolder}/env/bin/python3.6。如果您settings.json直接编辑而不使用 GUI:
{
"python.defaultInterpreterPath": "${workspaceFolder}/env/bin/python3.6"
}
Run Code Online (Sandbox Code Playgroud)
详细说明可以在文档“手动指定解释器”中找到,包括使用环境变量作为解释器的路径。
在VSCode中,python有两个路径:
出于理智的目的,您应该确保“Python:选择解释器”和系统环境变量都指向相同版本的 Python。
Windows 10 中的额外好东西。如果您没有设置环境变量,并且在 VSCode 终端中键入“python”,它将指向 C:\Users\YourName\AppData\Local\Microsoft\WindowsApps\python.exe,这只是在 Windows AppStore 中打开 python 链接。
This solution is for Mac and Linux:
To change your Python version from 2.7 to 3 do this:
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.
查找“ python”,并将其旁边的命令更改为“ python3”。
保存更改,您应该一切顺利。
查看您的屏幕截图,我发现您正在使用Code Runner扩展程序。我想这就是您启动程序的方式。我不知道它在内部如何工作,但是将此行添加到Code-Runner扩展setting.json文件中可以解决此问题:
"code-runner.executorMap.python": "python3 -u"
Run Code Online (Sandbox Code Playgroud)
在Code-Runner github存储库中找到它:https : //github.com/formulahendry/vscode-code-runner/issues/366
如果键入“ python --help”,您将看到“ -u”标志代表“无缓冲二进制stdout和stderr ...”-不知道为什么这很重要。
| 归档时间: |
|
| 查看次数: |
21489 次 |
| 最近记录: |