Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
当前回答
在Visual Studio Code (Python)的最新版本(1.36)中:
按F5,然后按Enter,在集成终端中运行代码。
按Ctrl + A,然后按Shift + Enter,在交互式IPython shell中运行代码。
其他回答
为了扩展vlad2135的答案(阅读他的第一个);这就是如何在Visual Studio Code中使用Don Jayamanne的Python扩展来设置Python调试(这是目前Python的一个功能非常全面的IDE,并且可以说是Visual Studio Code中最好的语言扩展之一,我认为)。
基本上,当你点击齿轮图标时,它会创建一个启动。Json文件在你的工作空间的。vscode目录。您也可以自己做这个,但是让Visual Studio Code来做这些繁重的工作可能会更简单。下面是一个示例文件:
生成后,您会注意到一些很酷的东西。它自动创建了一堆配置(我的大部分配置都被切断了;只要滚动就能全部看到),针对不同的库或环境(比如Django)有不同的设置和额外的功能。
你最终使用最多的可能是Python;这是一个简单的(在我的例子中是C)Python调试器,最容易使用设置。
我将对这个JSON属性做一个简短的介绍,因为其他的JSON属性使用几乎相同的配置,只是有不同的解释器路径和一两个不同的其他特性。
name: The name of the configuration. A useful example of why you would change it is if you have two Python configurations which use the same type of config, but different arguments. It's what shows up in the box you see on the top left (my box says "python" since I'm using the default Python configuration). type: Interpreter type. You generally don't want to change this one. request: How you want to run your code, and you generally don't want to change this one either. Default value is "launch", but changing it to "attach" allows the debugger to attach to an already running Python process. Instead of changing it, add a configuration of type attach and use that. stopOnEntry: Python debuggers like to have an invisible break-point when you start the program so you can see the entry-point file and where your first line of active code is. It drives some C#/Java programmers like me insane. false if you don't want it, true otherwise. pythonPath: The path to your install of Python. The default value gets the extension level default in the user/workspace settings. Change it here if you want to have different Pythons for different debug processes. Change it in workspace settings if you want to change it for all debug processes set to the default configuration in a project. Change it in user setting to change where the extension finds Pythons across all projects. (4/12/2017 The following was fixed in extension version 0.6.1). Ironically enough, this gets auto-generated wrong. It auto-generates to "${config.python.pythonPath}" which is deprecated in the newer Visual Studio Code versions. It might still work, but you should use "${config:python.pythonPath}" instead for your default first python on your path or Visual Studio Code settings. (4/6/2017 Edit: This should be fixed in the next release. The team committed the fix a few days ago.) program: The initial file that you debugger starts up when you hit run. "${workspaceRoot}" is the root folder you opened up as your workspace (When you go over to the file icon, the base open folder). Another neat trick if you want to get your program running quickly, or you have multiple entry points to your program is to set this to "${file}" which will start debugging at the file you have open and in focus in the moment you hit debug. cwd: The current working directory folder of the project you're running. Usually you'll just want to leave this "${workspaceRoot}". debugOptions: Some debugger flags. The ones in the picture are default flags, you can find more flags in the python debugger pages, I'm sure. args: This isn't actually a default configuration setting, but a useful one nonetheless (and probably what the OP was asking about). These are the command line arguments that you pass in to your program. The debugger passes these in as though they you had typed: python file.py [args] into your terminal; passing each JSON string in the list to the program in order.
有关Visual Studio Code文件变量的更多信息,可用于配置调试器和路径。
您可以到这里查看扩展自己的启动选项文档,包括可选属性和必选属性。
如果文件中没有配置模板,可以单击右下角的Add Configuration按钮。它将为您提供一个列表,以便为大多数常见的调试过程自动生成配置。
现在,根据vlad的回答,您可以按照正常的可视化调试器添加任何您需要的断点,在左上角的下拉菜单中选择您想要的运行配置,您可以点击配置名称左侧的绿色箭头来启动您的程序。
专业提示:团队中不同的人使用不同的ide,他们可能不需要您的配置文件。Visual Studio Code几乎总是把它的IDE文件放在一个地方(设计为这个目的;如果这是你第一次生成Visual Studio Code文件,请确保将.vscode/目录添加到你的.gitignore中(如果你还没有这个文件,这个过程将在你的工作空间中创建文件夹)!
注意:你必须在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.
在Mac上使用control+F5(fn+F5)
我使用Python 3.7(32位)。要在Visual Studio Code中运行程序,我右键单击程序并选择“在Python交互式窗口中运行当前文件”。如果你没有Jupyter,你可能会被要求安装它。
如果你有一个由多个Python文件组成的项目,并且你想用主程序开始运行/调试,而不依赖于当前的哪个文件,你可以创建以下启动配置(将MyMain.py更改为你的主文件)。
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Main File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/MyMain.py",
"console": "integratedTerminal",
"cwd": "${workspaceFolder}"
}
]
}