# 如何安裝 VScode 與設定 Python (Windows 10/11)
## Python 安裝
到 [Python 官方下載頁面](https://www.python.org/downloads/),往下滑找到 Python 3.9.13

點擊後進到 Python 3.9.13 頁面:

往下滑找到 [Windows installer 64 bit](https://www.python.org/ftp/python/3.9.13/python-3.9.13-amd64.exe),點擊下載

等待下載完畢,開啟檔案,進到安裝指引畫面
1. 勾選 <font color="pink"><strong>Add Python 3.9 to PATH</strong></font>
2. **Install Now**

安裝完成後,確認系統找得到 Python:
按 `win` 鍵 > 輸入 cmd,開啟小黑窗。
鍵入:
```shell
python -V
```
應該要看到:

要確認這次的**實驗是在 Python 3.9.13 執行**,如果不是的話,請看
或使用:
```shell
where python
```
會看到 Python39 在你的路徑當中。

### FAQ
#### 安裝後卻找不到 Python 3.9?
通常是因為忘記加入環境變數。
兩種辦法:
1. 解出安裝後重新安裝,記得勾選 <font color="pink"><strong>Add Python 3.9 to PATH</strong></font>
2. 貼下面的 script 到 PowerShell,將 Python 加入環境變數。
```sh
$Paths = "$env:USERPROFILE\AppData\Local\Programs\Python\Python39\Scripts\", "$env:USERPROFILE\AppData\Local\Programs\Python\Python39\"
$current = [Environment]::GetEnvironmentVariable("Path", "User").Split(';') | Where-Object { $_ -ne "" }
$filtered = $current | Where-Object { $Paths -notcontains $_ }
$updated = ($Paths + $filtered) -join ';'
[Environment]::SetEnvironmentVariable("Path", $updated, "User")
```
如果要查看現在的環境變數:
```sh
[Environment]::GetEnvironmentVariable("Path", "User")
```
#### 安裝之後版本不是 `3.9.13` ?
在環境變數中可能有若干個版本的 Python,要確認 Python 的路徑是在環境變數列表中最前面。
可以執行
```sh
[Environment]::GetEnvironmentVariable("Path", "User")
```
檢查環境變數。
並且用:
```sh
$Paths = "$env:USERPROFILE\AppData\Local\Programs\Python\Python39\Scripts\", "$env:USERPROFILE\AppData\Local\Programs\Python\Python39\"
$current = [Environment]::GetEnvironmentVariable("Path", "User").Split(';') | Where-Object { $_ -ne "" }
$filtered = $current | Where-Object { $Paths -notcontains $_ }
$updated = ($Paths + $filtered) -join ';'
[Environment]::SetEnvironmentVariable("Path", $updated, "User")
```
他會幫你把路徑移到最前面。在執行之後重啟終端機或 VScode,應該要可以看到版本是正確的。(**不用重新啟動**)
## Visual Studio Code 安裝
到[官方下載頁面](https://code.visualstudio.com/download):

下載檔案後開啟:
勾選同意

接著只要一路按下一步就行

最後點選安裝,等待安裝完畢

開啟 VScode 後:
1. 點擊左側 Extension 圖示
2. 在搜索框輸入 `python`
3. 安裝第一個套件

### 運行 Python 檔案
1. 在桌面或任何你能找到的地方創建資料夾
2. File > Open Folder > 選擇剛創建的資料夾

3. 在 EXPLORER 區域:右鍵 > New File

檔案名稱後綴為 `.py`,例如 test\.py。
4. 在檔案新增一些內容,例如:
```python=
import random
print(random.randint(30, 400))
```
5. 按下右上角 ▷ 執行 `.py`

最後,在下方 TERMINAL 會看到輸出結果:
