iT邦幫忙

3

使用VS code建置環境並執行python程式

Citrus 2019-07-11 19:53:26143989 瀏覽

下載並安裝python

首先到python官方網站
https://www.python.org/
因為我用的是Win10,所以選Windows的
https://ithelp.ithome.com.tw/upload/images/20190802/201188899q3ymAXKK8.png
下載完成後執行剛才的程式
https://ithelp.ithome.com.tw/upload/images/20190802/20118889wRZVKZQEsh.png
Add Python 3.7 to PATH勾起來,這樣python就會自動加到環境變數
接著就一直下一步,裝好後就可以關閉
開啟命令提示字元,輸入python,按Enter,如果有出現
https://ithelp.ithome.com.tw/upload/images/20190802/20118889ah8hqRFHzM.png
代表安裝成功
如果出現
https://ithelp.ithome.com.tw/upload/images/20190802/20118889izLuAI3RyE.png
代表環境變數沒有設定好

設定環境變數

電腦(右鍵)->內容->進階系統設定->環境變數->選取系統變數Path->編輯->加入指令路徑->確認設定
https://ithelp.ithome.com.tw/upload/images/20190802/201188893lzYrukESv.png
確認這兩個有被加進來
C:\Users\user\AppData\Local\Programs\Python\Python37-32\Scripts
C:\Users\user\AppData\Local\Programs\Python\Python37-32
注意,設定更改後,需重新開啟命令提示字元,變更才會生效喔!

建置VS code環境

開啟VS code->檔案->開啟資料夾->右鍵新建一個資料夾
https://ithelp.ithome.com.tw/upload/images/20190711/201188898VpWOdhhmw.png
建好後按選擇資料夾
https://ithelp.ithome.com.tw/upload/images/20190711/20118889iZc5fzdOcG.png
新增資料夾.VScode
https://ithelp.ithome.com.tw/upload/images/20190711/20118889SOotiwxaRm.png
https://ithelp.ithome.com.tw/upload/images/20190711/20118889uTjyP9J70g.png
在.vscode資料夾裡新增檔案settings.json
https://ithelp.ithome.com.tw/upload/images/20190711/201188893e0emQcLzs.png
https://ithelp.ithome.com.tw/upload/images/20190711/201188892YEjOIyhDZ.png
輸入以下程式碼,你的python路徑可能跟我的不太一樣

{
    "python.pythonPath": "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe",
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
}

https://ithelp.ithome.com.tw/upload/images/20190711/20118889Nd8l9FMt1h.png
按左下角那裡也可以看到目前設置的python路徑
https://ithelp.ithome.com.tw/upload/images/20190711/20118889pvVoRIi9xq.png
安裝Python這個延伸模組
https://ithelp.ithome.com.tw/upload/images/20190711/20118889dvIxB1Hucl.png
在資料夾裡新增檔案test.py
https://ithelp.ithome.com.tw/upload/images/20190711/20118889kcvFJVxDW4.png
可以開始輸入程式碼,按左下角開啟終端機
https://ithelp.ithome.com.tw/upload/images/20190711/20118889XH0VaPWSXr.png
在終端機中輸入指令

python test.py

執行python程式
https://ithelp.ithome.com.tw/upload/images/20190711/20118889xc3TdH2ZJc.png
接著安裝除錯程式,在終端機中輸入指令

pip install pylint

https://ithelp.ithome.com.tw/upload/images/20190711/201188899URVunRGMp.png
裝好後它就能夠幫你的python程式除錯,把print改成printf後也出現了錯誤訊息
https://ithelp.ithome.com.tw/upload/images/20190711/20118889TBBC0ntAQP.png
這就是為什麼在settings.json中要加入

"python.linting.enabled": true,
"python.linting.pylintEnabled": true,

因為這樣才能開啟除錯功能
如果你沒裝的話它可能就會出現這樣的錯誤訊息
https://ithelp.ithome.com.tw/upload/images/20190729/20118889OjXiMlYi2n.png
最後安裝code runner這個延伸模組
https://ithelp.ithome.com.tw/upload/images/20190713/20118889rCg5leRTRV.png
裝好後就可以按右鍵->run code直接執行程式
也可以按右上角的三角形按鈕來執行程式喔~
https://ithelp.ithome.com.tw/upload/images/20190711/201188893hkGU5MaGh.png
如果要更改字型或字體大小,檔案->喜好設定->設定 就可以進到設定頁面
https://ithelp.ithome.com.tw/upload/images/20190711/20118889CGi31Skinl.png
注意一下設定區的"使用者"這部分是VS code的全域設定
也就是會套用到所有的地方,不管從哪邊開啟檔案跟資料夾
如果只想改目前的資料夾的個人化設定
請改"工作區"這部分的,改完後就會發現.vscode目錄會多了json檔或是json檔被修改,那代表改好了
如果要把英文介面改為中文
按快捷鍵Ctrl+Shift+P
搜尋"語言"
https://ithelp.ithome.com.tw/upload/images/20190713/20118889wEIqVT7rC6.png
選擇設定顯示語言->按Enter->選擇zh-tw
介面就會變為中文了
為了要讓VS code能夠幫我們的python程式碼自動排版及格式化
在終端機中輸入指令

pip install autopep8

在settings.json中設定

{
    "python.pythonPath": "C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe",
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "files.trimTrailingWhitespace": true, // 儲存的時候,會幫你自動過濾多餘的空格
    "files.autoSave": "onFocusChange", // 是否自動儲存檔案
    "[python]":{  // 加入這幾行
        "editor.formatOnType": true,
        "editor.formatOnSave": true,
        "editor.renderIndentGuides": true,
        "editor.insertSpaces": true,
        "editor.detectIndentation": true,
        "editor.tabSize": 4
    },
}

裝好後就會發現每當你打完python的一行程式碼按Enter
以及存檔時
VS code就會幫我們的程式碼自動做排版


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 則留言

0
to790303
iT邦新手 5 級 ‧ 2019-11-04 17:53:26

您好,請問下為何我在settings.json裡面的設置,如同你圖片中的設置一樣,但是他的訊息都會告知尾句有逗號,然後顯示有錯誤,是為什麼會這樣呢?

netenken1 iT邦新手 4 級 ‧ 2022-07-12 23:51:21 檢舉

我也有遇到, 將結尾的逗號拿掉即可, 我想應該是後來python的語法檢查認定最後面有逗號是不對的

我要留言

立即登入留言