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的右上角,你会看到运行按钮和 打击。 之后,你会在vs code的底部看到你的代码已经被执行。 您可以为“代码运行器”创建自己的键盘快捷键,以加快编码速度。
其他回答
到目前为止,在Visual Studio Code中运行Python有四种方式:
通过集成终端(拜托,它是集成的!所以从技术上讲,你可以在Visual Studio Code中运行它;)
不需要安装任何扩展。 不需要创建和配置任何东西(假设您的$PATH中已经有python)。 ⌃空格(打开终端)和python my_file.py(运行文件)。
通过自定义任务(接受芬顿的回答):
不需要安装任何扩展。 默认的Visual Studio Code做事的方式。 注意不要复制粘贴答案,因为它的problemMatcher.pattern.regexp已经损坏,并且挂起了编辑器。最好要么删除problemMatcher,要么将regexp至少更改为^\\s+(.*)$。
通过代码运行扩展(@JanHan的答案):
需要配置代码运行器。在用户设置中的executorMap(添加路径到您的python)。 非常有用的扩展,特别是如果你在Visual Studio代码中不仅运行Python。
通过微软的官方Python扩展(vlad2135的答案):
需要创建launch.js(在Visual Studio Code的Debug选项卡中单击几下)。 对于那些想要使用Visual Studio Code作为Python的主要IDE的人来说,这个扩展是必不可少的。
注意:你必须在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.
您不再需要任何额外的扩展。您可以简单地将调试器的输出切换到集成终端。
Ctrl+Shift+D,然后从顶部的下拉菜单中选择集成终端/控制台。
我使用我现有的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
超级简单:
按下F5键,代码将运行。
如果设置了断点,按F5将在断点处停止,并在调试模式下运行代码。
其他方法-添加快捷方式
注意:你必须在Visual Studio Code中安装Python Extension By Microsoft,并在左下角选择Python解释器。
选择文件→首选项→键盘快捷键(也可以按Ctrl + K + S) 在搜索框中输入python. execterminal 双击该结果(或者,可以单击加号图标) 按Ctrl + Alt + B将其注册为键绑定(或者,您可以输入自己的键绑定)
现在您可以关闭键盘快捷键选项卡 转到您想要运行的Python文件,并按Ctrl + Alt + B(或者,您可以按下您设置的键绑定)来运行它。输出将显示在底部的终端选项卡中。