---
# System prepended metadata

title: Python on Windows

---

# Pyen
---
* Windows: https://github.com/pyenv-win/pyenv-win
* Linux/Mac: https://github.com/pyenv/pyenv
```
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

```
pyenv-win 安裝
---
* https://chocolatey.org/install
* 使用 Powershell(以系統管理者執行)
```
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
```
# pyenv-win 安裝
1. * 使用 cmd 命令提示字元(以系統管理者執行)
1. * choco install pyenv-win
1. * 使用 Powershell(以系統管理者執行)
1. * 依序ㄧ行ㄧ行執行以下指令：
```
[System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
```
```
[System.Environment]::SetEnvironmentVariable('PYENV_ROOT',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
```
```
[System.Environment]::SetEnvironmentVariable('PYENV_HOME',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")
```
```
[System.Environment]::SetEnvironmentVariable('path', $env:USERPROFILE + "\.pyenv\pyenv-win\bin;" + $env:USERPROFILE + "\.pyenv\pyenv-win\shims;" + [System.Environment]::GetEnvironmentVariable('path', "User"),"User")
```
# pyenv-win 使用
1. 開啟 cmd 命令提示字元
1. pyenv versions
1. pyenv install --list 列出所有可安裝的 python 版本
1. pyenv install 3.10.5
1. pyenv versions 列出本機已安裝的所有 python 版本
1. pyenv global 3.10.5 設定預設 python 版本
1. cd 到專案目錄
1. pyenv local 3.8.10 設定當前目錄使用 python 3.8
1. python -V
# windows 基本指令
* dir 對應 Linux ls
# Python 虛擬環境
1. 確認 python 版本 python -V
1. 建立虛擬環境 python -m venv .venv
1. 進入虛擬環境 .venv\Scripts\activate
# Python 套件管理
基本用法
1. 新增requirements.txt
1. 新增套件寫進 requirements.txt
```
requests==2.31.0
```
# 進階用法
1. kdir requirements
1. cd requirements
1. 新增檔案 base.txt
```
requests==2.31.0
```
1. pip install -r .\requirements\base.txt
1. pip install -r .\requirements\base.txt -r .\requirements\gpu.txt
