diff --git a/content/windows-5-py.md b/content/windows-5-py.md index 14add7c..ba325cd 100644 --- a/content/windows-5-py.md +++ b/content/windows-5-py.md @@ -184,77 +184,100 @@ pip install jupyter-notebook-translation > 当然,你也可以使用其他编辑器/IDE如 Sublime Text 或者 JetBrains 系列的 PyCharm 。 -## 使用UV替代Conda -> UV(由 Astral 团队开发)是一个用 Rust 编写的高性能包管理器,提供了类似 Conda 的虚拟环境管理和依赖解析功能,并且在大多数场景下比 pip 和 Conda 快 10–100 倍。它通过命令行工具如 uv venv(创建/管理虚拟环境)和 uv pip(安装/锁定/同步依赖)来覆盖传统的 conda create、conda install、conda env export 等操作,但本身并不管理底层的 C/C++ 库,因此对于诸如 GDAL、SciPy 等需要系统级二进制依赖的包,仍建议在 Conda/系统包管理器中预装相关库,然后用 UV 来管理 Python 包。 +## 使用 UV 替代 Conda -**安装与激活** -``` +> UV(由 Astral 团队开发)是一个用 Rust 编写的高性能 Python 包管理器,提供类似 Conda 的虚拟环境管理和依赖解析功能,在大多数场景下比 pip 和 Conda 快 10–100 倍。它通过命令行工具如 `uv venv`(创建/管理虚拟环境)和 `uv pip`(安装/锁定/同步依赖)覆盖传统的 Conda 流程,但本身不管理底层的 C/C++ 库,因此对于 GDAL、SciPy 等需要系统级二进制依赖的包,仍建议先通过系统包管理器或 Conda 安装,然后用 UV 管理 Python 包。 + +--- + +- 安装 UV + +```bash wget -qO- https://astral.sh/uv/install.sh | sh ``` -- 在当前目录下创建 .venv,使用系统默认 Python(若不存在则自动下载) -``` -uv venv -``` -- 指定环境名称或路径 -``` -uv venv myenv -``` -- 指定 Python 版本(需系统已有或可下载) -``` -uv venv --python 3.11 -``` -- 激活 -``` + +- 创建与管理环境 + +```bash +# 创建虚拟环境,指定 Python 版本 +uv venv --python 3.12 + +# 激活环境 source .venv/bin/activate -``` -**安装包** -```bash -# 安装单个包 -uv pip install requests +# 退出环境 +deactivate -# 批量安装并自动锁定依赖 -uv pip install fastapi uvicorn sqlalchemy +# 删除环境 +rm -rf .venv ``` -**生成与同步锁文件** +- 直接运行 ```bash -# 从 requirements.in 生成统一依赖文件 -uv pip compile docs/requirements.in \ - --universal \ - --output-file docs/requirements.txt - -# 根据锁文件同步环境 -uv pip sync docs/requirements.txt +uv run python +uv run jupyter lab ``` -此流程替代 `conda env export` + `conda env update`,并保证跨平台一致性 ([GitHub][3])。 - -**查看与卸载** +- 注册 Jupyter 内核 ```bash -uv pip list # 列出已安装包(类似 conda list) +uv run python -m ipykernel install --user --name bank --display-name "Python (bank)" +``` + +--- + +- 安装依赖 + +```bash +uv add tensorflow +uv pip install requests fastapi uvicorn sqlalchemy +``` + +> 安装完成后,UV 会自动更新 `uv.lock` 文件锁定依赖版本,保证环境可复现。 + + +- 使用 TOML 配置管理依赖 + +创建一个 `pyproject.toml`: + +```toml +[tool.uv.dependencies] +fastapi = "*" +uvicorn = "*" +sqlalchemy = "*" +``` + +然后同步环境: + +```bash +uv pip sync +``` + +这会根据 `pyproject.toml` + `uv.lock` 安装和锁定所有依赖。 + + +- 查看与卸载包 + +```bash +uv pip list # 列出已安装包 uv pip uninstall numpy ``` -**替代常见 Conda 工作流** +--- -| Conda 操作 | UV 对应 | -| -------------------------------- | ---------------------------------------- | -| `conda create -n env python=3.x` | `uv venv --python 3.x` | -| `conda activate env` | `source .venv/bin/activate` 或 `activate` | -| `conda install pkg1 pkg2` | `uv pip install pkg1 pkg2` | -| `conda env export > env.yml` | `uv pip compile requirements.in` | -| `conda env update -f env.yml` | `uv pip sync requirements.txt` | -| `conda list` | `uv pip list` | +### 替代常见 Conda 工作流 -**最佳实践**: +| Conda 操作 | UV 对应 | +| -------------------------------- | ------------------------------------------------- | +| `conda create -n env python=3.x` | `uv venv --python 3.x` | +| `conda activate env` | `source .venv/bin/activate` 或 `uv venv activate` | +| `conda install pkg1 pkg2` | `uv pip install pkg1 pkg2` | +| `conda env export > env.yml` | 自动生成 `uv.lock` 或 `uv pip compile requirements.in` | +| `conda env update -f env.yml` | `uv pip sync`(根据 `uv.lock` 或 `pyproject.toml` 同步) | +| `conda list` | `uv pip list` | -1. **系统依赖**:用 Conda/Mamba 安装较难编译的 C 库(`conda install gdal`)。 -2. **Python 包**:用 UV 管理所有纯 Python 依赖(`uv pip install pandas scikit-learn`)。 -3. **统一锁定**:把 `uv pip compile` 生成的 `requirements.txt` 放入版本控制,确保团队环境一致。 ## ipynb转markdown diff --git a/public/pagefind/fragment/en_8a2fd78.pf_fragment b/public/pagefind/fragment/en_8a2fd78.pf_fragment new file mode 100644 index 0000000..991563c Binary files /dev/null and b/public/pagefind/fragment/en_8a2fd78.pf_fragment differ diff --git a/public/pagefind/fragment/en_ac931a2.pf_fragment b/public/pagefind/fragment/en_ac931a2.pf_fragment deleted file mode 100644 index 16e2f6e..0000000 Binary files a/public/pagefind/fragment/en_ac931a2.pf_fragment and /dev/null differ diff --git a/public/pagefind/index/en_2b9cf43.pf_index b/public/pagefind/index/en_2b9cf43.pf_index deleted file mode 100644 index ce1a32e..0000000 Binary files a/public/pagefind/index/en_2b9cf43.pf_index and /dev/null differ diff --git a/public/pagefind/index/en_2bcd86d.pf_index b/public/pagefind/index/en_2bcd86d.pf_index new file mode 100644 index 0000000..0cdab52 Binary files /dev/null and b/public/pagefind/index/en_2bcd86d.pf_index differ diff --git a/public/pagefind/index/en_5aed6aa.pf_index b/public/pagefind/index/en_5aed6aa.pf_index new file mode 100644 index 0000000..de1762f Binary files /dev/null and b/public/pagefind/index/en_5aed6aa.pf_index differ diff --git a/public/pagefind/index/en_753eeda.pf_index b/public/pagefind/index/en_753eeda.pf_index new file mode 100644 index 0000000..d1bcffc Binary files /dev/null and b/public/pagefind/index/en_753eeda.pf_index differ diff --git a/public/pagefind/index/en_867953b.pf_index b/public/pagefind/index/en_867953b.pf_index new file mode 100644 index 0000000..644b4f4 Binary files /dev/null and b/public/pagefind/index/en_867953b.pf_index differ diff --git a/public/pagefind/index/en_8e11b0e.pf_index b/public/pagefind/index/en_8e11b0e.pf_index deleted file mode 100644 index 44f8ec7..0000000 Binary files a/public/pagefind/index/en_8e11b0e.pf_index and /dev/null differ diff --git a/public/pagefind/index/en_903572b.pf_index b/public/pagefind/index/en_903572b.pf_index deleted file mode 100644 index 6e5c746..0000000 Binary files a/public/pagefind/index/en_903572b.pf_index and /dev/null differ diff --git a/public/pagefind/index/en_ad76273.pf_index b/public/pagefind/index/en_ad76273.pf_index new file mode 100644 index 0000000..dc85f83 Binary files /dev/null and b/public/pagefind/index/en_ad76273.pf_index differ diff --git a/public/pagefind/index/en_d9025a4.pf_index b/public/pagefind/index/en_d9025a4.pf_index deleted file mode 100644 index 0a2b231..0000000 Binary files a/public/pagefind/index/en_d9025a4.pf_index and /dev/null differ diff --git a/public/pagefind/index/en_e930797.pf_index b/public/pagefind/index/en_e930797.pf_index deleted file mode 100644 index c53d2d1..0000000 Binary files a/public/pagefind/index/en_e930797.pf_index and /dev/null differ diff --git a/public/pagefind/pagefind-entry.json b/public/pagefind/pagefind-entry.json index e285853..ca50a3b 100644 --- a/public/pagefind/pagefind-entry.json +++ b/public/pagefind/pagefind-entry.json @@ -1 +1 @@ -{"version":"1.3.0","languages":{"en":{"hash":"en_40173c68cb","wasm":"en","page_count":77}}} \ No newline at end of file +{"version":"1.3.0","languages":{"en":{"hash":"en_81338c9b9e","wasm":"en","page_count":77}}} \ No newline at end of file diff --git a/public/pagefind/pagefind.en_40173c68cb.pf_meta b/public/pagefind/pagefind.en_40173c68cb.pf_meta deleted file mode 100644 index 12095ed..0000000 Binary files a/public/pagefind/pagefind.en_40173c68cb.pf_meta and /dev/null differ diff --git a/public/pagefind/pagefind.en_81338c9b9e.pf_meta b/public/pagefind/pagefind.en_81338c9b9e.pf_meta new file mode 100644 index 0000000..639e3ca Binary files /dev/null and b/public/pagefind/pagefind.en_81338c9b9e.pf_meta differ diff --git a/public/windows-5-py/index.html b/public/windows-5-py/index.html index eec64b5..8fc9b26 100644 --- a/public/windows-5-py/index.html +++ b/public/windows-5-py/index.html @@ -306,69 +306,81 @@

当然,你也可以使用其他编辑器/IDE如 Sublime Text 或者 JetBrains 系列的 PyCharm 。

-

使用UV替代Conda

+

使用 UV 替代 Conda

-

UV(由 Astral 团队开发)是一个用 Rust 编写的高性能包管理器,提供了类似 Conda 的虚拟环境管理和依赖解析功能,并且在大多数场景下比 pip 和 Conda 快 10–100 倍。它通过命令行工具如 uv venv(创建/管理虚拟环境)和 uv pip(安装/锁定/同步依赖)来覆盖传统的 conda create、conda install、conda env export 等操作,但本身并不管理底层的 C/C++ 库,因此对于诸如 GDAL、SciPy 等需要系统级二进制依赖的包,仍建议在 Conda/系统包管理器中预装相关库,然后用 UV 来管理 Python 包。

+

UV(由 Astral 团队开发)是一个用 Rust 编写的高性能 Python 包管理器,提供类似 Conda 的虚拟环境管理和依赖解析功能,在大多数场景下比 pip 和 Conda 快 10–100 倍。它通过命令行工具如 uv venv(创建/管理虚拟环境)和 uv pip(安装/锁定/同步依赖)覆盖传统的 Conda 流程,但本身不管理底层的 C/C++ 库,因此对于 GDAL、SciPy 等需要系统级二进制依赖的包,仍建议先通过系统包管理器或 Conda 安装,然后用 UV 管理 Python 包。

-

安装与激活

-
wget -qO- https://astral.sh/uv/install.sh | sh
+
+
    +
  • 安装 UV
  • +
+
wget -qO- https://astral.sh/uv/install.sh | sh
 
    -
  • 在当前目录下创建 .venv,使用系统默认 Python(若不存在则自动下载)
  • +
  • 创建与管理环境
-
uv venv
-
-
    -
  • 指定环境名称或路径
  • -
-
uv venv myenv
-
-
    -
  • 指定 Python 版本(需系统已有或可下载)
  • -
-
uv venv --python 3.11
-
-
    -
  • 激活
  • -
-
source .venv/bin/activate
-
-

安装包

-
# 安装单个包
-uv pip install requests
+
# 创建虚拟环境,指定 Python 版本
+uv venv --python 3.12
 
-# 批量安装并自动锁定依赖
-uv pip install fastapi uvicorn sqlalchemy
-
-

生成与同步锁文件

-
# 从 requirements.in 生成统一依赖文件
-uv pip compile docs/requirements.in \
-   --universal \
-   --output-file docs/requirements.txt
+# 激活环境
+source .venv/bin/activate
 
-# 根据锁文件同步环境
-uv pip sync docs/requirements.txt
+# 退出环境
+deactivate
+
+# 删除环境
+rm -rf .venv
 
-

此流程替代 conda env export + conda env update,并保证跨平台一致性 ([GitHub][3])。

-

查看与卸载

-
uv pip list       # 列出已安装包(类似 conda list)
+
    +
  • 直接运行
  • +
+
uv run python
+uv run jupyter lab
+
+
    +
  • 注册 Jupyter 内核
  • +
+
uv run python -m ipykernel install --user --name bank --display-name "Python (bank)"
+
+
+
    +
  • 安装依赖
  • +
+
uv add tensorflow
+uv pip install requests fastapi uvicorn sqlalchemy
+
+
+

安装完成后,UV 会自动更新 uv.lock 文件锁定依赖版本,保证环境可复现。

+
+
    +
  • 使用 TOML 配置管理依赖
  • +
+

创建一个 pyproject.toml

+
[tool.uv.dependencies]
+fastapi = "*"
+uvicorn = "*"
+sqlalchemy = "*"
+
+

然后同步环境:

+
uv pip sync
+
+

这会根据 pyproject.toml + uv.lock 安装和锁定所有依赖。

+
    +
  • 查看与卸载包
  • +
+
uv pip list          # 列出已安装包
 uv pip uninstall numpy
 
-

替代常见 Conda 工作流

+
+

替代常见 Conda 工作流

- + - - + +
Conda 操作UV 对应
conda create -n env python=3.xuv venv --python 3.x
conda activate envsource .venv/bin/activateactivate
conda activate envsource .venv/bin/activateuv venv activate
conda install pkg1 pkg2uv pip install pkg1 pkg2
conda env export > env.ymluv pip compile requirements.in
conda env update -f env.ymluv pip sync requirements.txt
conda env export > env.yml自动生成 uv.lockuv pip compile requirements.in
conda env update -f env.ymluv pip sync(根据 uv.lockpyproject.toml 同步)
conda listuv pip list
-

最佳实践

-
    -
  1. 系统依赖:用 Conda/Mamba 安装较难编译的 C 库(conda install gdal)。
  2. -
  3. Python 包:用 UV 管理所有纯 Python 依赖(uv pip install pandas scikit-learn)。
  4. -
  5. 统一锁定:把 uv pip compile 生成的 requirements.txt 放入版本控制,确保团队环境一致。
  6. -

ipynb转markdown

首先安装 nbformat 和 nbconvert包:

conda install nbformat nbconvert -y