Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
Visual Studio Code最近发布了,我喜欢它的外观和提供的功能,所以我想尝试一下。
我从下载页面下载了应用程序,启动了它,对一些功能做了一些调整……然后意识到我不知道如何实际执行我的任何Python代码!
我真的很喜欢Visual Studio代码的外观和感觉/可用性/特性,但我似乎不知道如何运行我的Python代码,这是一个真正的杀手,因为我主要用Python编程。
是否有一种方法可以在Visual Studio code中执行Python代码?
当前回答
为了使用相应的venv启动当前文件,我在launch.json文件中添加了以下内容:
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"pythonPath": "${workspaceFolder}/FOO/DIR/venv/bin/python3"
},
在bin文件夹中存在源代码…/venv/bin/activate脚本,当从常规终端运行时,该脚本是常规来源。
其他回答
以下是当前(2018年9月)用于运行Python代码的扩展:
官方Python扩展:这是必须安装的。
代码运行器:对各种语言都非常有用,不仅仅是Python。我强烈推荐安装。
实时Python便签,在侧窗口中显示你的变量。我是这个的创建者,所以显然我认为它很棒,但我不能给出一个公正的意见¯\()/¯
Wolf:实时Python便签,内联显示结果
当然,如果你使用集成的终端,你可以在那里运行Python代码,而不必安装任何扩展。
为了扩展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中(如果你还没有这个文件,这个过程将在你的工作空间中创建文件夹)!
有一个最简单的方法来创建run in terminal命令的快捷方式:
点击左边栏上的设置图标。 然后点击键盘快捷键。 粘贴python。execInTerminal在搜索栏顶部。 现在双击Python: Run Python File in Terminal命令对面的Keybinding列并设置快捷方式。
安装Python扩展(Python应该安装在您的系统中)。要安装Python扩展,请按Ctrl + Shift + X,然后输入' Python '并输入。安装扩展。 打开包含Python代码的文件。是的!一个.py文件。 现在要运行.py代码,只需在编辑器屏幕上右键单击并点击“在终端中运行Python文件”。就是这样!
这是额外的一步。事实上,一次又一次的点击让我很恼火,所以我设置了快捷键。
点击左下角的“设置类型”图标→键盘快捷键→键入“在终端中运行Python文件”。现在你会看到+号,然后选择快捷方式。你已经完成了!
一个解决办法是:
选择所有的行:按Ctrl + A 运行选定的行:Shift + Enter