2019-01-18

安裝及使用 Visual Studio Code (VSCode) 撰寫 python 程式


====================================================================
好用的 VS code extension:
python
PYQT Integration
indent-rainbow / python indent
Bracker Pair Colorizer 2
Live Server
Kite --- see https://www.youtube.com/watch?v=W--_EOzdTHk
AREPL --- see https://www.youtube.com/watch?v=W--_EOzdTHk
emmet
Material Icon
Prettier NOW
Intelisense and Peek
Snipset
Paste JSON as Code
REST Client : allows you to send HTTP request and view the response in Visual Studio Code directly.
Git Lend
SSH FS
IntelliCode

====================================================================
安裝 PyQt5 and PyQt5-tools ( or PyQt5designer, 有何不同? )

因為 pyqt5 5.10 之後不再提供常用的 Qt 工具, 如 Qt designer, linguist...,必須另外安裝 pyqt5-tools.

若先安裝 pyqt5, 在安裝 pyqt5-tools 有可能會發生版本衝突的問題. 建議直接安裝 pyqt5-tools, 它會連帶安裝適合的 pyqt5 版本.

pip install --pre pyqt5-tools~=5.13

====================================================================
在 VSCode 中安裝 PyQt Integration extension
若不安裝 PyQt5-tools, 而是使用獨立安裝的 QT designer, 可開啟, 但查看 help>plugins 時, 會有以下錯誤訊息:
Failed Plugins
L:/python/projects/pyqt5gui/venv_381/lib/stite-packages/pyqt5_tools/Qt/bin/plugins/designer/pyqt5.dll

Cannot load library  L:/python/projects/pyqt5gui/venv_381/lib/stite-packages/pyqt5_tools/Qt/bin/plugins/designer/pyqt5.dll : The specified module could not be found.

可用的設定如下:
{
"folders": [
{
"path": "."
}
],
"settings": {
"python.venvPath": "~/venv_381",
"pyqt-integration.qtdesigner.path": "L:\\python\\Projects\\PyQT5GUI\\venv_381\\scripts\\pyqt5designer.exe",
"pyqt-integration.pyuic.cmd": "L:\\python\\Projects\\PyQT5GUI\\venv_381\\scripts\\pyuic5",
"pyqt-integration.pyrcc.cmd": "L:\\python\\Projects\\PyQT5GUI\\venv_381\\scripts\\pyrcc5",
"pyqt-integration.pylupdate.cmd": "L:\\python\\Projects\\PyQT5GUI\\venv_381\\scripts\\pylupdate5",
"pyqt-integration.linguist.cmd": "L:\\python\\Projects\\PyQT5GUI\\venv_381\\scripts\\linguist"
}
}

若 "pyqt-integration.qtdesigner.path": 設為 designer.exe, 也會有同樣的問題. 可參考 pyqt5-tools PyPI
若 "pyqt-integration.qtdesigner.path": "~\\venv_381\\scripts\\pyqt5designer.exe", 則會找不到 pyqt5designer.exe, 一定要設定絕對路徑


====================================================================
安裝 / 設定 Pylint
pip install pylint
vscode user settings.json :

{
    "workbench.startupEditor": "newUntitledFile",
    "telemetry.enableTelemetry": false,
    "telemetry.enableCrashReporter": false,
    "window.zoomLevel": 0,
    "pyqt-integration.qtdesigner.path": "c:\\Program Files (x86)\\Qt Designer\\designer.exe",
    "indentRainbow.includedLanguages": [
        "python"
    ],
    "python.linting.pylintArgs": [
        "--errors-only",
        "--extension-pkg-whitelist=PyQt5"
    ],
}

其中 "python.linting.pylintArgs" 的設定, 也可在專案目錄下的 .pylintrc 撰寫, 內容為 :
errors-only
extension-pkg-whitelist=PyQt5

=====================================================================
0. 主程式安裝位置: C:\user\cliff\Appdata\Local\Programs\Microsoft vs code

1. 主程式設定檔 : c:\user\cliff\AppData\Roaming\Code\User\settings.json

      EX:
     {
          "workbench.startupEditor": "newUntitledFile",
          "telemetry.enableCrashReporter": false,
          "telemetry.enableTelemetry": false 
     }

2. extensions 储存位置 : c:\user\cliff\.vscode\extensions\
     目前只安裝 Microsoft python extension

3. folder 設定檔 : L:\python\Projects\Training\.vscode\settings.json
     EX:
    {
         "python.pythonPath": ""L:\\python\\Projects\\Training\\venv_01\\Scripts\\python.exe"
    }

4. workspace 設定檔 : L:\python\Projects\Training\TrainingWK.code-workspace
    ex:
{
 "folders": [
  {
   "path": "."
  },
  {
   "path": "L:\\python\\Projects\\PycharmProjects\\py3book31"
  }
 ],
 "settings": {}
}

設定檔的存在與否及內容, 會隨著設定的位置不同 (user/workspace/folder) 而有所差異.

以設定 python virtual environment 為例:

VSCode > setting > search "python path" > under workspace : modify python: Venv Path 輸入 L:\\python\\Projects\\PyQt5\\venv_381\\Scripts\\python.exe 即可讓 python environment 開始發揮作用. 此時, 沒有 .vscode 目錄. 僅有 PyQt5.code-workspace 檔案, 注意其內容已改變了.

.vscode\settings.json --- settings > folder > 修改設定, 自動產生或修改
.vscode\launch.json --- 啟動 debug, 會詢問是否產生
TrainingWK.code-workspace --- settings > workspace > 修改設定, 自動產生或修改
.idea --- ?

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.