[Visual Studio Code] 执行python

Visual Studio Code 作为一种IDE,实时执行python程序,对调试或理解执行步骤起到了很大的作用!因此,以下对此作一简单接受,希望广大园友提出宝贵意见!

1. 官方说明

http://code.visualstudio.com/docs/editor/tasks

2. 配置执行环境

  Note: Please note that task support is only available when working on a workspace folder. It is not available when editing single files.

  2.1 配置task.json

    a. 创建一个workspace folder

    b. ctrl+shift+p -> task -> Tasks: Configure Task Runner(配置执行任务)

    c. 点击“配置任务运行程序”,弹出一下窗口

Output Window Behavior#

Sometimes you will want to control how the output window behaves when running tasks. For instance, you may want to maximize editor space and only look at task output if you think there is a problem. The property showOutput controls this and the valid values are:

  • always - The output window is always brought to front. This is the default.
  • never - The user must explicitly bring the output window to the front using the View > Toggle Output command (Ctrl+Shift+U).
  • silent - The output window is brought to front only if no problem matchers are set for the task.

command and tasks[]#

tasks.json takes a single command value which can be a task runner like gulp or grunt or any command line tool like a compiler or linter. By default the command will show up in the Tasks: Run Taskdropdown.

You can also define multiple tasks in a tasks array in order to pass different arguments or use different settings when the command is run.

Here‘s a simple example passing different arguments to the echo command:

 1 {
 2     "version": "0.1.0",
 3     "command": "echo",
 4     "isShellCommand": true,
 5     "args": [],
 6     "showOutput": "always",
 7     "echoCommand": true,
 8     "suppressTaskName": true,
 9     "tasks": [
10         {
11             "taskName": "hello",
12             "args": ["Hello World"]
13         },
14         {
15             "taskName": "bye",
16             "args": ["Good Bye"]
17         }
18     ]
19 }

Some tasks.json properties such as showOutput and suppressTaskName can be set both globally and then overridden in specific tasks. The tasks args property values are appended to the global arguments.

There are also tasks specific properties. One useful property is isBuildCommand, which if set to true, will run the task with the Tasks: Run Build Task (Ctrl+Shift+B) command.

Running multiple commands#

What if you want to run different command line tools in your workspace? Defining multiple tasks in tasks.json is not yet fully supported by VS Code (see #981). You can work around this limitation by running your task commands through a shell command (sh on Linux and Mac, cmd on Windows).

Here is an example to add two tasks for make and ls:

 1 {
 2     "version": "0.1.0",
 3     "command": "sh",
 4     "args": ["-c"],
 5     "isShellCommand": true,
 6     "showOutput": "always",
 7     "suppressTaskName": true,
 8     "tasks": [
 9         {
10             "taskName": "make",
11             "args": ["make"]
12         },
13         {
14             "taskName": "ls",
15             "args": ["ls"]
16         }
17     ]
18 }

Both tasks make and ls will be visible in the Tasks: Run Task dropdown.

For Windows, you will need to pass the ‘/C‘ argument to cmd so that the tasks arguments are run.

    "command": "cmd",
    "args": ["/C"]

Variable substitution#

When authoring tasks configurations, it is often useful to have a set of predefined common variables. VS Code supports variable substitution inside strings in the tasks.json file and has the following predefined variables:

  • ${workspaceRoot} the path of the folder opened in VS Code
  • ${workspaceRootFolderName} the name of the folder opened in VS Code without any solidus (/)
  • ${file} the current opened file
  • ${relativeFile} the current opened file relative to workspaceRoot
  • ${fileBasename} the current opened file‘s basename
  • ${fileDirname} the current opened file‘s dirname
  • ${fileExtname} the current opened file‘s extension
  • ${cwd} the task runner‘s current working directory on startup

You can also reference environment variables through ${env.Name} (e.g. ${env.PATH}). Be sure to match the environment variable name‘s casing, for example env.Path on Windows.

Below is an example of a configuration that passes the current opened file to the TypeScript compiler.

1 {
2     "command": "tsc",
3     "args": ["${file}"]
4 }

Operating System Specific Properties#

The task system supports defining values (for example, the command to be executed) specific to an operating system. To do so, simply put an operating system specific literal into the tasks.jsonfile and specify the corresponding properties inside that literal.

Below is an example that uses the Node.js executable as a command and is treated differently on Windows and Linux:

1 {
2     "version": "0.1.0",
3     "windows": {
4         "command": "C:\\Program Files\\nodejs\\node.exe"
5     },
6     "linux": {
7         "command": "/usr/bin/node"
8     }
9 }

Valid operating properties are windows for Windows, linux for Linux and osx for Mac. Properties defined in an operating system specific scope override properties defined in the global scope.

In the example below:

1 {
2     "version": "0.1.0",
3     "showOutput": "never",
4     "windows": {
5         "showOutput": "always"
6     }
7 }
8 Output from the executed task is never brought to front except for Windows where it is always shown.
时间: 2024-08-10 19:19:08

[Visual Studio Code] 执行python的相关文章

visual studio code编辑python文件

visual studio code 安装.通过360软件管家,查找visual studio code 下载安装即可 设置visual studio code为中文 打开进入软件,Ctrl + Shift + P,切入到命令行模式.输入“Configure Language” 然后点击下拉框出来的 Configure Display Language 然后会出现如下界面,把"locale":"en"改成"locale":"zh-CN&

Visual Studio Code搭建python开发环境

开发Python的环境有很多,原来已经在vs2013上面搭建好python的开发环境了,但是vs2013每次启动都占太多内存(太强大了吧),这下出了vs code,既轻量又酷炫,正好拿来试一试开发python,点击visual studio code1.9安装教程 下面直接上搭建Python环境步骤: 1.打开vs code,按按F1或者Ctrl+Shift+P打开命令行,然后输入ext install 输入Python,选第一个,这个用的最多,支持自动补全代码等功能,点击安装按钮,即可安装 下

Visual Studio Code安装Python环境

如何在全宇宙最强编辑器安装Python运行环境 (雾 首先安装Python2和Python3,如果只需要用到一个的话,直接安装即可运行,不存在转换问题. 安装Python扩展,直接搜索安装即可. 更改.json配置如下图 setting.json中搜索python.pythonPath,之后将pythonPath更改为python3即可.(如果没有python3,则进入安装目录下把python3文件夹下的python.exe改为python3.exe,估计就可以了.mac和linux下应该不存在

visual studio code 里调试运行 Python代码

最近对微软的visual studio code 挺感兴趣的,微软的跨平台开发工具.轻量简洁. 版本迭代的也挺快的,现在已近1.3.1版本了,功能也愈加完善. 今天就简单描述一下我在vs code里怎么运行调试Python的. 因为在网上没怎么看到过相关的文章描述.我就自己摸索,把基本的环境搭建步骤给记录一下. 一.下载安装vscode最新版本,目前是1.3.1版本.地址自行搜索,这个都不会搜,那说明对vscode没有了解和兴趣.就不用看了. 安装完成后打开vscode,按图示安装Python的

Python编辑器设置(包括Visual Studio Code和Sublime Text3)

源起 Python强制缩进,使用Tab键和空格都可以,但不能混用.混用Tab和空格会导致报错:IndentationError: unindent does not match any outer indentation level.如果你的编辑器没有可视化空格和Tab,修改起来是抓狂的. PEP8推荐使用空格来缩进.因为不同的编辑器.IDE对Tab键的处理不一样,有的宽度为4,有的为8. 配置 下面修改一下Visual Studio Code和Sublime Text3的配置,让空格和Tab键

使用 Visual Studio Code(VSCode)搭建简单的 Python + Django 开发环境

写在前面的话 作为有个 Python 菜逼,之前一直用的 Pycharm,但是在主题这一块怎么调整都感觉要么太骚,看起来不舒服,要么就是简直不能看.似乎用大 JB 公司 IDE 的人似乎都不怎么重视主题这个东西. 我个人是那种“喜新厌旧”的,所以看久了 Pycharm 就感觉很难受了,再加上 Pycharm 太笨重了,就换成了以前短时间用过的 VSCode,以前没用是懒得折腾插件,现在闲下来准备折腾一下! 这里放一张我现在的 Pycharm 的配色主题: 闲话少说,进入正题! 创建 Django

使用 Visual Studio Code创建和执行T-SQL

安装mssql扩展 在扩展中搜索"sql",在搜索结果中选择"SQL Server (mssql)" 创建sql文件 File > New File 或者使用快捷键 Ctrl+N. Visual Studio Code 默认会打开一个文本文件. 在下方的状态条中选择 Plain Text ,然后搜索"sql",在语言下拉框中选择sql . 连接SQL Server 快捷键 Ctrl+Shift+P 或 F1 打开命令面板. 输入sql展示m

Visual Studio Code Python 环境部署

Visual Studio Code Python 环境部署 简单说两句 一直以来都在用 PyCharm,但是随着学生身份的消失,不能合法免费使用了.而社区版本的 PyCharm 功能又太不完善.当然,这些还不是主要问题,因为 PyCharm 太好用,这些都不是阻碍我继续使用 PyCharm 的理由. 现在遇到的问题是,电脑配置太低,虚拟机开着 Ubuntu,里面跑两个 PyCharm 很快就吃不消了.看着进度条一卡一卡的,十分闹心. 一直以来就听说 VS Code 这东西靠谱,今天开始尝试这转

Visual Studio上开发Python六大功能

一.整合 Python 直译器 (Interpreter) & 互动视窗 (Interactive) Visual Studio 高度整合 Python 直译器,让您能够在开发过程中切换不同版本的 Python 直译器.此项功能除了能够切换至您所熟悉的 Python 版本进行开发外,更可确保您的程序在不同 Python 版本下运行的函式相容性是合法的,如下图代码当中的 print 函式,在 Python 2.7 环境下为合法的 (红色箭头指向目前为使用 Python 2.7 全域环境直译器).