使用 Visual Studio Code開發Python

一開始在選擇python開發工具的時候有看到很多選擇,像是PyCharm or Sublime Text,但最後選擇了VS code

這篇文章主要依照https://zhuanlan.zhihu.com/p/59709499,來安裝VS code 的環境

  1. 確定Python3已經安裝
python3

Python 3.7.4 (default, Sep 7 2019, 18:27:02)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

2.安裝必要的套件

Python 
Django
Jinja
MagicPython //Syntax highlighter for cutting edge Python.
Python Extension Pack
Python Path
Anaconda Extension Pack //會調高亮度,補全和提示Python的關鍵字
Bracket Pair Colorizer //括號生成不同颜色,能醒目的區分括號
indent-rainbow //對縮排有顏色提示
vscode-icons //VSCode的icon主题,快速區分檔案種類

如何在VS Code 工具中執行 Python
Ref: http://www.weithenn.org/2018/05/python-journey-part02-how-to-use-vscode.html

  1. Run Python File in Terminal

在編輯的 .py 檔案中按下滑鼠右鍵選擇 “Run Python File in Terminal”,VS Code 就會在下方的 Terminal 中執行 Python 檔案。

2.在 VS Code 工具中依序點選Tasks > Configure Tasks選項後,組態設定 tasks.json 內容,依照官網的說明修改參數

最重要就是要在”command”添加${config:python.pythonPath}參數來執行python

然後就可以透過 Run Build Task 方式執行 Python,下面是我寫的範例

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Python",
"type": "shell",
"command": "${config:python.pythonPath}",
"args": [
"${file}"
],
"presentation": {
"reveal": "always",
"panel": "shared"
},
"group": {
"kind": "build",
"isDefault": true
}
}
]
}

3.可以透過 Debug mode來執行該檔案

Written by

YAML engineer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store