Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
当前回答
我使用我现有的anaconda环境来运行python。而不是使用python用户appdata\local\programs\python使用anaconda install python by environment。这将使您能够访问环境中的所有库。
1. View->Command Palette->Open user settings
2. search python
a. Python: default interpreter path = c:\users\yourname\Anaconda3\python.exe
b. save the file
3. View->Command Palette->python:select interpreter
a. arrow down to your workspace name
b. select your python and environment
create a python script and run it.
see https://code.visualstudio.com/docs/python/environments
其他回答
从扩展,安装代码运行器。在此之后,您可以使用快捷方式在Visual Studio code中运行源代码。
首先:运行代码:
使用快捷键Ctrl + Alt + N 或按F1,然后选择/键入运行代码, 或在文本编辑器窗口中单击右键,然后单击编辑器上下文菜单中的运行代码 或单击编辑器标题菜单中的运行代码按钮(右边的三角形) 或在文件资源管理器上下文菜单中单击“运行代码”。
第二步:停止运行的代码:
使用快捷键Ctrl + Alt + M 或按F1,然后选择/键入停止代码运行 或右键单击“输出通道”,然后单击上下文菜单中的“停止代码运行”
在Python for Visual Studio Code扩展中有一个在终端中运行Python文件命令。
有一个更简单的方法来运行Python,它不需要任何配置:
安装代码运行器扩展。 在文本编辑器中打开Python代码文件。 运行Python代码:
使用快捷键Ctrl + Alt + N 或按F1,然后选择/键入运行代码, 或者右键单击“文本编辑器”,然后在编辑器上下文菜单中单击“运行代码” 或单击编辑器标题菜单中的“运行代码”按钮 或单击文件资源管理器上下文菜单中的运行代码按钮
停止正在运行的代码:
使用快捷键Ctrl + Alt + M 或按F1,然后选择/键入停止代码运行 或右键单击“输出通道”,然后单击上下文菜单中的“停止代码运行”
如果你想添加Python路径,你可以在File→Preference→Settings中添加Python路径,如下所示:
"code-runner.executorMap":
{
"python": "\"C:\\Program Files\\Python35\\python.exe\" -u"
}
如果你已经安装了Python扩展并手动设置了解释器,你可以配置你的设置。Json文件如下:
{
"python.pythonPath": "C:\\\\python36\\\\python36.exe",
"code-runner.executorMap":
{
"python": "$pythonPath -u $fullFileName"
}
}
注意:你必须在Visual Studio Code中安装Python Extension By Microsoft,并在左下角选择Python解释器。
Go to File → Preferences → Keyboard Shortcuts (alternatively, you can press Ctrl + K + S) In the search box, enter python.execInTerminal Double click that result (alternatively, you can click the plus icon) Press Ctrl + Alt + B to register this as the keybinding (alternatively, you can enter your own keybinding) Now you can close the Keyboard Shortcuts tab Go to the Python file you want to run and press Ctrl + Alt + B (alternatively, you can press the keybinding you set) to run it. The output will be shown in the bottom terminal tab.
为了使用相应的venv启动当前文件,我在launch.json文件中添加了以下内容:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"pythonPath": "${workspaceFolder}/FOO/DIR/venv/bin/python3"
},
在bin文件夹中存在源代码…/venv/bin/activate脚本,当从常规终端运行时,该脚本是常规来源。