Python 项目和包管理器 uv 安装以及使用
This commit is contained in:
parent
c6c972b287
commit
136ed4e9a5
114
source/_posts/【教程】Python 项目和包管理器 uv 安装以及使用.md
Normal file
114
source/_posts/【教程】Python 项目和包管理器 uv 安装以及使用.md
Normal file
@ -0,0 +1,114 @@
|
||||
---
|
||||
title: 【教程】Python 项目和包管理器 uv 安装以及使用
|
||||
date: 2024/11/9 13:12
|
||||
tags: [windows, linux, uv, python, venv]
|
||||
categories: 技术
|
||||
permalink: 380.html
|
||||
index_img: https://i.dawnlab.me/bf3a9545e37daac5afbd662a9e265b4a.png
|
||||
---
|
||||
|
||||
![UV.png](https://i.dawnlab.me/bf3a9545e37daac5afbd662a9e265b4a.png)
|
||||
|
||||
# 介绍
|
||||
|
||||
[官网](https://docs.astral.sh/uv/)
|
||||
|
||||
uv 是一个用 Rust 编写的 Python 包安装器和解析器。它旨在成为 pip 和 pip-tools 工作流的直接替代品。Astral 的开发者们的目标是创建一个 “Python 的 Cargo”,这是一个全面的 Python 项目和包管理器,速度快、可靠且易于使用。
|
||||
|
||||
# 安装
|
||||
|
||||
## 国内安装优化
|
||||
|
||||
### 添加环境变量
|
||||
|
||||
使用 GitHub 镜像网站替换掉默认地址,加速下载
|
||||
|
||||
```env
|
||||
UV_INSTALLER_GHE_BASE_URL=https://ghproxy.cn/https://github.com
|
||||
UV_PYTHON_INSTALL_MIRROR=https://ghproxy.cn/https://github.com/indygreg/python-build-standalone/releases/download
|
||||
```
|
||||
|
||||
默认使用清华 pypi 源
|
||||
|
||||
```env
|
||||
UV_DEFAULT_INDEX=https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
|
||||
```
|
||||
|
||||
## Windows
|
||||
|
||||
```powershell
|
||||
powershell -ExecutionPolicy ByPass -c "irm https://ghproxy.cn/https://github.com/astral-sh/uv/releases/latest/download/uv-installer.ps1 | iex"
|
||||
```
|
||||
|
||||
## Linux
|
||||
|
||||
```bash
|
||||
curl -LsSf https://ghproxy.cn/https://github.com/astral-sh/uv/releases/latest/download/uv-installer.sh | sh
|
||||
```
|
||||
|
||||
# 使用
|
||||
|
||||
## 创建项目
|
||||
|
||||
```bash
|
||||
uv init hello-world
|
||||
cd hello-world
|
||||
```
|
||||
|
||||
或者
|
||||
|
||||
```bash
|
||||
mkdir hello-world
|
||||
cd hello-world
|
||||
uv init
|
||||
```
|
||||
|
||||
## 创建虚拟环境
|
||||
|
||||
```bash
|
||||
uv venv .venv
|
||||
```
|
||||
|
||||
指定 Python 版本号,未安装时会自动下载预构建的 Python 安装。
|
||||
|
||||
```bash
|
||||
uv venv .venv --python=3.12
|
||||
```
|
||||
|
||||
## 添加依赖
|
||||
|
||||
uv 会自动解决依赖关系。
|
||||
|
||||
```bash
|
||||
uv add httpx
|
||||
```
|
||||
|
||||
## 同步依赖环境
|
||||
|
||||
```bash
|
||||
uv sync
|
||||
```
|
||||
|
||||
同时包含所有可选依赖:
|
||||
|
||||
```bash
|
||||
uv sync --all-extras
|
||||
```
|
||||
|
||||
## 导出为传统依赖文件 requirements.txt
|
||||
|
||||
```bash
|
||||
uv export -o requirements.txt
|
||||
```
|
||||
|
||||
不包含 hashes :
|
||||
|
||||
```bash
|
||||
uv export -o requirements.txt --no-hashes
|
||||
```
|
||||
|
||||
同时包含所有可选依赖:
|
||||
|
||||
```bash
|
||||
uv export -o requirements.txt --no-hashes --all-extras
|
||||
```
|
Loading…
Reference in New Issue
Block a user