首頁  >  文章  >  後端開發  >  如何在VSCode中加入Python解釋器並安裝Python庫

如何在VSCode中加入Python解釋器並安裝Python庫

PHPz
PHPz轉載
2023-05-10 18:52:2013880瀏覽

一.安裝VScode編輯器

雙擊「VSCodeUserSetup-x64-1.74.2.exe」安裝包;選擇「我同意此協定」後點選下一步;

如何在VSCode中加入Python解釋器並安裝Python庫

選擇預設路徑並點擊下一步;

如何在VSCode中加入Python解釋器並安裝Python庫

#選擇下一步;

如何在VSCode中加入Python解釋器並安裝Python庫

#勾選「建立桌面快捷方式」並點選下一步;

如何在VSCode中加入Python解釋器並安裝Python庫

點選安裝;

如何在VSCode中加入Python解釋器並安裝Python庫

安裝結束後點選完成;至此VScode安裝成功;

如何在VSCode中加入Python解釋器並安裝Python庫

二.安裝Python解釋器

雙擊“python-3.7.2-amd64(1).exe”,並勾選Add Python 3.7 to PATH”後選擇「Install Now」;

如何在VSCode中加入Python解釋器並安裝Python庫

等待安裝進度條載入完;

如何在VSCode中加入Python解釋器並安裝Python庫

安裝成功後點選Close;至此Python解釋器安裝成功;

如何在VSCode中加入Python解釋器並安裝Python庫

三.在VScode中加入Python解釋器

a) 在桌面雙擊安裝好的VScode圖標,進入VScode軟體介面;

如何在VSCode中加入Python解釋器並安裝Python庫

b) 安裝Python外掛程式:先點選“擴充”,並在搜尋框中輸入Python,然後選擇Python外掛右下角的Install;

如何在VSCode中加入Python解釋器並安裝Python庫

c) 新增Python解釋器:按快捷鍵「Ctrl Shift P」,調出全域設定搜尋窗口,然後輸入「Python:Select Interpreter」後會出現「Python:Select Interpreter」選項,點選該選項;

如何在VSCode中加入Python解釋器並安裝Python庫

d) 點擊該選項會後跳到Python解釋器配置窗口,這裡顯示的是已經新增好的Python解釋器;若要新增Python解釋器需要點選「 Enter interpreter path…”選項,選擇之後進入到下一個視窗;

如何在VSCode中加入Python解釋器並安裝Python庫

e) 在此視窗點擊”Find”選項開啟檔案總管;

如何在VSCode中加入Python解釋器並安裝Python庫

f) 然後找到需要新增的Python3.7.2解釋器安裝目錄(即第二步驟安裝Python解釋器的預設路徑),並選擇「Python.exe」之後再點選「Select Interpreter」按鈕,此時已將Python3.7.2解釋器新增至VScode編輯器之中;

如何在VSCode中加入Python解釋器並安裝Python庫

注意事項:
若Python安裝路徑下找不到“AppDate”資料夾則需要進行該操作:依序點擊“查看”—>“顯示”—>“隱藏的項目” ,即可找到「AppDate」資料夾

如何在VSCode中加入Python解釋器並安裝Python庫

g) 新增完Python3.7.2解釋器後,需重新使用快捷鍵「Ctrl Shift P」開啟全域設定視窗並進入「Python:Select Interpreter」選項,若該頁面有Python3.7.2解釋器即表示新增成功;

如何在VSCode中加入Python解釋器並安裝Python庫

四.建立專案並在VScode中開啟;

i. 在桌面新資料夾,並命名為「AI-case」;

如何在VSCode中加入Python解釋器並安裝Python庫

ii. 在VScode中,點擊”File”,並選擇”Open Folder”,將桌面新建的“AI-case”加入;

如何在VSCode中加入Python解釋器並安裝Python庫

##iii. 新增「AI-case」後點選確定,並選擇「Yes,I trust the authors」;

如何在VSCode中加入Python解釋器並安裝Python庫

iv. 在新增進來的「AI-case」專案中點選「New fiel…」新text.py;

如何在VSCode中加入Python解釋器並安裝Python庫

v. 在建立好的text.py中輸入「print(‘ hello world’)”,並點擊運行按鈕,即可在終端得到運行結果;

如何在VSCode中加入Python解釋器並安裝Python庫

五、在VScode中安裝Python庫

確定在VScode中新增了Python3.7.2解釋器:可在VScode右下角看到如下圖所示的內容則表示正確選擇了Python3.7.2;

如何在VSCode中加入Python解釋器並安裝Python庫

在編輯欄中輸入“

import requests“,並點擊運行按鈕,則會出現以下錯誤:” ModuleNotFoundError: No module named “requests”,因此需要安裝requests庫;

如何在VSCode中加入Python解釋器並安裝Python庫

點擊運行按鈕,運行text.py文件,從而在

Terminal#中得到Python3.7.2的安裝路徑;

如何在VSCode中加入Python解釋器並安裝Python庫

在Terminal中進入Python3.7.2安裝目錄下的Scripts資料夾:輸入cd 空格Python3.7.2的安裝路徑\Scripts\;敲迴車;注意在複製Python3.7.2的安裝路徑時不要複製python. exe

如何在VSCode中加入Python解釋器並安裝Python庫

確定之後若Terminal直接出現了資料夾Scripts的路徑,則輸入:「.

\pip install 需要安裝函式庫名稱",(例如:.\pip install requests),回車後等待安裝成功即可;

如何在VSCode中加入Python解釋器並安裝Python庫#

以上是如何在VSCode中加入Python解釋器並安裝Python庫的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除