如何設定 Python 環境
警告:
在這篇指引的Linux部分,只適用於基於Debian的發行版,例如Debain、Ubuntu、Linux Mint和elementary OS等。
如果你使用的發行版所的套件管理工具不是apt
,你需要轉換所有使用到apt
的指令到你使用發行版的套件管理工具的版本,按這裡查看如何轉換。
0. 安裝PowerShell(只限Windows)
1. 至PowerShell在Github的最終發行頁面下載適用於Windows的msi安裝檔,參考以下圖片的紅框:

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

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

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

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
警告: 這邊指的PowerShell是在步驟0時安裝的PowerShell(或pwsh.exe),並不是內建於Windows的傳統Windows PowerShell(或powershell.exe)。要開啟PowerShell,你可以:
開啟開始選單,到PowerShell開啟PowerShell 7 (x64),如果安裝了版本為7的PowerShell。
使用鍵盤快速鍵
⊞ Win
+R
開啟執行對話框,輸入pwsh然後按確定.使用Windows Search搜尋pwsh就可以找到。
,然後你就可以看到視窗中的第一行必須像:
(base) PS C:\Users\User>
如果是的話,Miniconda已經成功安裝在Windows了。
2. 安裝Visual Studio Code
1. 至Visual Studio Code官方網站按下Download for Windows來下載安裝檔,參考以下圖片的紅框:

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

3. Install Git
4. 設置並更新conda
1. 開啟PowerShell(Windows)或終端機(Linux)。
2. 執行以下指令來檢查conda的版本:
conda --version
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
3. 一行一行的執行以下指令將你的全名和信箱設置到Git的全域設置:
git config --global user.name "<fullname>"
git config --global user.email "<email>"
6. 建立一個用於在Visual Studio Code進行開發的conda環境dev
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
列出所有在建立新環境時會安裝的套件,它們有:
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",
}
警告:
如果你是使用Windows,你需要將<install location of PowerShell>
取代成你安裝的PowerShell的安裝路徑並將\
跳脫至\\
。
例如如果你的PowerShell是安裝在預設路徑C:\Program Files\PowerShell,你只需要將<install location of PowerShell>
取代成C:\\Program Files\\PowerShell
,就如同以下圖片:

6. 重開Visual Studio Code。
8. 在Visual Studio Code中執行Python腳本
1. 藉由以下方式在Visual Studio Code中建立一個新的檔案:
位於Welcome頁面的New file連結。
在選單列選擇File→New 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!
。
最后更新于
这有帮助吗?