如何設定 Python 環境

提示: 在這篇指引的Windows部分,使用User作為使用者名稱。

0. 安裝PowerShell(只限Windows)

1. 至PowerShell在Github的最終發行頁面下載適用於Windows的msi安裝檔,參考以下圖片的紅框:

提示: 適用於Windows的msi安裝檔的命名規則為PowerShell-<version>-win-x64.msi。 如果PowerShell最終發行版本為7.0.2,選擇PowerShell-7.0.2-win-x64.msi

2. 開啟安裝檔來安裝PowerShell,確保在安裝過程中每個選項都和以下圖片一致:

提示: PowerShell的預設安裝位置為C:\Program Files\PowerShell,如果你想改的話也可以。

1. 安裝Miniconda

1. 至Miniconda官方網站按下Miniconda3 Windows 64-bit來下載安裝檔,參考以下圖片的紅框:

2. 開啟安裝檔來安裝Miniconda,確保在安裝過程中每個選項都和以下圖片一致:

提示: Miniconda的預設安裝位置為C:\Users\<username>\miniconda3,如果你想改的話也可以。

3. 開啟開始選單,到Anaconda3 (64-bit)開啟Anaconda Powershell Prompt (Miniconda3),並一行一行的執行以下指令:

conda init
cd "$([Environment]::GetFolderPath('Personal'))\PowerShell"
Write-Output "chcp 65001`ncls" | Out-File "Profile.ps1" "utf8" -Append

4. 關閉Anaconda Powershell Prompt (Miniconda3)並開啟PowerShell

,然後你就可以看到視窗中的第一行必須像:

(base) PS C:\Users\User>

2. 安裝Visual Studio Code

1. 至Visual Studio Code官方網站按下Download for Windows來下載安裝檔,參考以下圖片的紅框:

2. 開啟安裝檔來安裝Visual Studio Code,確保在安裝過程中每個選項都和以下圖片一致:

提示: Visual Studio Code的預設安裝位置為C:\Users\<username>\AppData\Local\Programs\Microsoft VS Code,如果你想改的話也可以。

3. Install Git

1. 至Git官方網站按下Download <version> for Windows來下載安裝檔,參考以下圖片的紅框:

2. 開啟安裝檔來安裝Git,確保在安裝過程中每個選項都和以下圖片一致:

提示: Git的預設安裝位置為C:\Program Files\Git,如果你想改的話也可以。

4. 設置並更新conda

1. 開啟PowerShell(Windows)或終端機(Linux)。

2. 執行以下指令來檢查conda的版本:

 conda --version

提示: 如果執行失敗,你需要重新安裝Miniconda。

3. 一行一行的執行以下指令來設置並更新conda:

 conda config --set channel_priority strict
 conda update --all -y
  • config --set channel_priority strict表示設置頻道的優先度至嚴格,按這裡取得更多有關頻道優先度的細節。

  • update --all -y表示在當前的conda環境中更新所有套件(現在是base),且不需要任何確認。

5. 設置Git

1. 開啟PowerShell(Windows)或終端機(Linux)。

2. 執行以下指令來檢查Git的版本:

 git --version

提示: 如果執行失敗,你需要重新安裝Git。

3. 一行一行的執行以下指令將你的全名和信箱設置到Git的全域設置:

 git config --global user.name "<fullname>"
 git config --global user.email "<email>"

如果你的全名是王小明,且你的信箱是wang@example.com,你需要執行:

 git config --global user.name "王小明"
 git config --global user.email "wang@example.com"

6. 建立一個用於在Visual Studio Code進行開發的conda環境dev

1. 開啟PowerShell(Windows)或終端機(Linux)。

2. 執行Execute以下指令來建立一個conda環境:

 conda create -n dev --strict-channel-priority -y python=3 jupyter pylint autopep8 rope
  • create表示建立一個conda環境。

  • -n dev表示環境的名稱是dev

  • --strict-channel-priority表示在建立一個新的環境時,所有安裝的套件會遵守嚴格的頻道優先度。

  • -y表示不需確認。

  • python=3 jupyter pylint autopep8 rope列出所有在建立新環境時會安裝的套件,它們有:

    • python=3:Python 3。

    • jupyter:Jupyter,包含IPython和Jupyter Notebook,按這裡取得更多細節。

    • pylint:用於Python的程式碼分析工具,在Visual Studio Code中為必要,按這裡取得更多細節。

    • autopep8:用於Python的格式化工具,在Visual Studio Code中為必要,按這裡取得更多細節。

    • rope::用於Python的重構工具,在Visual Studio Code中為必要,按這裡取得更多細節。

3. 執行以下指令來啟用環境dev

 conda activate dev

這指令會在環境base之上啟用dev

4. 執行以下指令來設置環境dev的頻道優先度至嚴格:

 conda config --env --set channel_priority strict

這指令會將當前環境(現在是dev)的頻道優先度設置成嚴格。

5. 執行以下指令使當前環境失效:

 conda deactivate

這指令會將環境dev失效回到base

7. 設置Visual Studio Code

1. 開啟Visual Studio Code,你可以透過以下方式開啟:

  • 開始選單(Windows)或應用程式概覽(Linux)。

  • 開啟命令提示字元PowerShell(Windows)或終端機(Linux),並執行code

    在你開啟之後,你應該要看到像以下圖片的介面:

2. 按照以下圖片的指示去安裝Python延伸模組:

3. 藉由以下方式開啟Visual Studio Code的設定:

  • 按照以下圖片的指示:

  • 使用鍵盤快速鍵Ctrl+,

4. 按下位於Visual Studio Code右上角的Open Settings (JSON)按鈕,參考以下圖片的紅框:

5. 複製以下內容至編輯區:

 {
     "terminal.external.windowsExec": "<install location of PowerShell>\\7\\pwsh.exe",
     "terminal.integrated.shell.windows": "<install location of PowerShell>\7\\pwsh.exe",
     "terminal.integrated.shellArgs.windows": [
         "-NoLogo"
     ],
     "python.pythonPath": "~/miniconda3/envs/dev/python",
 }

提示: 如果你是使用Linux,請忽略第2至6行,只需要:

 {
     "python.pythonPath": "~/miniconda3/envs/dev/python",
 }

6. 重開Visual Studio Code。

8. 在Visual Studio Code中執行Python腳本

1. 藉由以下方式在Visual Studio Code中建立一個新的檔案:

  • 位於Welcome頁面的New file連結。

  • 在選單列選擇FileNew File

  • 使用鍵盤快速鍵Ctrl+N。

2. 在編輯區寫下以下腳本:

 print('Hello, World!')

這個腳本表示會在終端印出Hello, World!

3. 儲存至一個Python檔案(持有檔案副檔名.py),就如同以下圖片中的helloworld.py

4. 按下位於Visual Studio Code右上角的Run Python File in Terminal按鈕,參考以下圖片的紅框:

按下了以後,你應該會看到像以下圖片的結果:

你可以看到終端區中有Hello, World!

最后更新于

这有帮助吗?