跳到主要內容
← 回到學習

學習 · 程式語言

Python Self learning

目錄 · 7 個段落
  1. 01確認 Python 版本與架構
  2. 02Windows cmd 常用指令
  3. 03環境管理:2026 推薦做法
  4. 04常用 ML / 多媒體套件
  5. 05簡繁轉換(個人常用)
  6. 06Python 版本選擇
  7. 07進階 Python 主題(尚未深入)

Python 自學筆記

TL;DR
個人 Python 環境設定 + 常用指令 + ML / 多媒體相關套件記錄。2026 年首選包管理 uv(Rust 寫的,比 pip 快 10-100 倍),環境管理用 uv 或 conda ,別再用 venv + pip。

確認 Python 版本與架構

python --version            # 看版本
python                      # 進 REPL,顯示 32-bit / 64-bit
bash
import platform
platform.architecture()     # ('64bit', 'WindowsPE')
platform.python_version()   # '3.12.1'
platform.system()           # 'Windows' / 'Linux' / 'Darwin'
python

Windows cmd 常用指令

:: 切換磁碟 + 路徑(`/d` flag 必加)
cd /d D:\GitProject
 
:: 不加 /d 只能切目前磁碟內路徑
cmd

環境管理:2026 推薦做法

首選:uv(Rust-based,超快)

# 安裝(Windows PowerShell)
irm https://astral.sh/uv/install.ps1 | iex
 
# 創建專案
uv init my-project
cd my-project
 
# 加套件
uv add pandas numpy matplotlib
 
# 跑 script
uv run python script.py
 
# 鎖版本
uv lock
bash

為什麼用 uv:

  • 速度比 pip 快 10-100 倍
  • 整合 venv + pip + pip-tools 三件事
  • 自動處理 Python 版本(內建 pyenv 替代)
  • 2024 出的新工具,2026 已成主流

次選:Conda(科學計算 / ML)

Bobo 個人 setup:

  • Anaconda 環境(含 opencc-python-reimplemented 做簡繁轉換)
  • 跑 script 用 conda run -n python3 python ...(不硬編絕對路徑,各電腦 Anaconda 安裝位置不同)
  • 查 env 清單:conda info --envs
# 建環境
conda create -n myenv python=3.11
 
# 啟用
conda activate myenv
 
# 安裝套件(優先用 conda-forge)
conda install -c conda-forge numpy pandas
 
# 看所有 env
conda info --envs
 
# 跨電腦跑 script(避免硬編路徑)
conda run -n python3 python my_script.py
bash

Conda 適合:ML / DS、需要 binary dependencies(CUDA / OpenCV / FFmpeg)、跨平台 toolchain。

已過時:venv + pip(別用了)

# 古早做法,2026 不推薦
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
bash

問題:慢、依賴解析弱、Python 版本切換麻煩。換用 uv。


常用 ML / 多媒體套件

Theano(已停更,別新用)

# Windows 上 Theano 配 g++ 的歷史問題
conda install m2w64-toolchain
conda install libpython
bash
Theano 已退場
2017 後沒大版本更新,功能被 PyTorch / TensorFlow / JAX 完全取代。新專案 絕對不要用 Theano。

現代深度學習選擇:

Framework適合維護
PyTorch研究、學界主流✅ Meta
TensorFlow生產、行動裝置✅ Google
JAX高效能科學計算✅ Google
Keras 3high-level API✅(包 PyTorch / TF / JAX)

FFmpeg(音訊 / 視訊處理)

pip install ffmpeg-python
# 或
uv add ffmpeg-python
bash

Python 包裝的 FFmpeg,用來:

  • 錄影 / 轉檔(MP4 / WebM / MOV)
  • 音訊提取
  • 串流處理
  • 縮時 / 截圖
import ffmpeg
 
(
    ffmpeg
    .input('input.mp4')
    .output('output.webm', vcodec='libvpx-vp9')
    .run()
)
python

注意:pip install ffmpeg-python 只是 Python wrapper,系統還要裝 FFmpeg 二進位:

# Windows
choco install ffmpeg
 
# Mac
brew install ffmpeg
bash

簡繁轉換(個人常用)

pip install opencc-python-reimplemented
bash
from opencc import OpenCC
 
cc = OpenCC('s2twp')        # 簡體 → 繁體(台灣標準,含詞彙轉換)
print(cc.convert('用户登录'))  # 使用者登入
 
cc = OpenCC('t2s')          # 繁體 → 簡體
print(cc.convert('使用者登入'))  # 用户登录
python

常用配置 ID:

  • s2t — 簡 → 繁(無詞彙轉換)
  • t2s — 繁 → 簡
  • s2twp — 簡 → 繁(台灣用語+詞彙轉換,例 视频→影片)
  • tw2sp — 繁 → 簡( 反向,影片→视频)

Python 版本選擇

版本狀態何時用
3.8 / 3.9EOL / 即將 EOL別新用
3.10LTS-ish舊專案維護
3.11 / 3.12主流預設選這個
3.13(2024 release)最新嘗鮮(有 free-threaded mode)

2026 年寫新 Python code 直接用 3.12+。3.12 的 Per-Interpreter GIL 為將來真正多核做準備。


進階 Python 主題(尚未深入)

下次補的清單
  • async/await 完整理解
  • Type hints + mypy / pyright
  • dataclass / Pydantic v2 資料驗證
  • Decorator 進階用法
  • Context manager(with / __enter__ / __exit__)
  • Metaclass / __init_subclass__
  • Concurrency(threading / multiprocessing / asyncio 三套對比)

目錄