搜索
首页后端开发Python教程使用 uv 管理 Python 环境

使用 uv 管理 Python 环境

Jan 08, 2025 pm 06:16 PM

使用 uv 管理 Python 環境

告别繁琐的 Python 环境管理!uv 是一款高效、便捷的工具,能一站式解决 Python 版本管理、虚拟环境创建、包管理以及项目管理等问题,速度快,上手简单。本文将以 Windows PowerShell 为例,演示 uv 的使用方法,其他平台可参考官方文档进行相应调整。

安装 uv

uv 不依赖 Python,因此不建议使用 pip 或 pipx 安装。Windows 系统可直接通过 PowerShell 执行以下命令安装:

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

或使用 scoop 等软件包管理器安装:

scoop install uv

使用 uv 管理多版本 Python

使用 uv python list 命令查看可安装和已安装的 Python 版本:

# uv python list
cpython-3.13.1+freethreaded-windows-x86_64-none    <download available="">
cpython-3.13.1-windows-x86_64-none                 <download available="">
cpython-3.12.8-windows-x86_64-none                 <download available="">
...

安装最新版本:

# uv python install
Installed Python 3.13.1 in 5.89s
+ cpython-3.13.1-windows-x86_64-none

查看安装结果:已安装版本会显示安装路径。

# uv python list
cpython-3.13.1-windows-x86_64-none                 C:\Users\meebo\AppData\Roaming\uv\python\cpython-3.13.1-windows-x86_64-none\python.exe
...

获取 Python 安装路径:

# uv python dir
C:\Users\meebo\AppData\Roaming\uv\python

安装指定版本:

# uv python install 3.10
Installed Python 3.10.16 in 9.78s
+ cpython-3.10.16-windows-x86_64-none

卸载 Python 版本 (需指定版本):

# uv python uninstall 3.10
Searching for Python versions matching: Python 3.10
Uninstalled Python 3.10.16 in 1.52s
- cpython-3.10.16-windows-x86_64-none

查看所有版本 (包含所有修订版本): uv python list --all-versions

安装多个版本:uv python install 3.10 3.11

卸载多个版本:uv python uninstall 3.10 3.11

使用 uv 替代 python/pip 工具

uv 管理的 Python 环境不能直接用 python 命令执行,需通过 uv run 命令执行。例如:

# cat .\show_version.py
import sys
print(sys.version)

执行:

# uv run .\show_version.py
3.13.1 (main, Dec 19 2024, 14:38:48) [MSC v.1942 64 bit (AMD64)]

指定 Python 版本执行:uv run --python 3.10 .show_version.py

从标准输入执行:echo 'print("hello world!")' | uv run -

查看已安装的 Python 版本:uv python list --only-installed

设置默认 Python 版本 (仅限当前目录):uv python pin 3.10 (创建 .python-version 文件)

指定执行时需要的包

如果程序需要额外包,例如 cowsay

# cat .\cow.py
from cowsay import cow
cow('hello, world')

使用 --with 选项指定包:

# uv run --with cowsay .\cow.py
Installed 1 package in 13ms
...

清除缓存的虚拟环境:uv cache clean

管理虚拟环境

创建虚拟环境:uv venv --python 3.10 (创建 .venv 目录) 或指定目录名: uv venv myenv

使用指定虚拟环境:uv run --python myenv .show_version.py

删除虚拟环境:删除虚拟环境目录

管理包

使用 uv pip 命令管理包,它与 pip 命令兼容。

安装包:uv pip install cowsay

查看包依赖关系:uv pip tree

卸载包:uv pip uninstall rich (不会自动删除不再需要的依赖包)

使用 uv 管理 Python 项目

uv 提供两种项目管理方式:单文件项目和文件夹项目。

单文件项目

初始化单文件项目:uv init --script cow3.py --python 3.13 (在 cow3.py 文件中添加元数据)

添加包:uv add --script cow3.py cowsay rich (修改 cow3.py 文件元数据)

移除包:uv remove --script cow3.py rich (修改 cow3.py 文件元数据)

文件夹项目

初始化文件夹项目:uv init myproject (创建项目目录,包含 .gitignore, .python-version, hello.py, pyproject.toml, README.md)

执行项目:uv run hello.py (创建 .venv 虚拟环境)

添加包:uv add cowsay rich (修改 pyproject.toml 文件)

更新包:uv lock --upgrade-package cowsayuv lock --upgrade

移除包:uv remove cowsay

同步项目环境与 uv.lock 文件:uv sync

查看项目包依赖关系:uv tree

使用包提供的工具命令

直接执行包命令:uvx cowsay -t 'hello, uv'uv tool run cowsay -t 'hello, uv'

指定包执行命令:uvx --from httpie http -p=b GET https://flagtech.github.io/flag.txt

安装包命令到系统:uv tool install httpie

更新包命令:uv tool upgrade httpie

卸载包命令:uv tool uninstall httpie

uv 提供了高效便捷的 Python 环境管理方案,显著提升开发效率。 通过本文的介绍,相信您已经掌握了 uv 的基本使用方法,可以更好地管理您的 Python 项目和环境。

以上是使用 uv 管理 Python 环境的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?列表和阵列之间的选择如何影响涉及大型数据集的Python应用程序的整体性能?May 03, 2025 am 12:11 AM

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

说明如何将内存分配给Python中的列表与数组。说明如何将内存分配给Python中的列表与数组。May 03, 2025 am 12:10 AM

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

您如何在Python数组中指定元素的数据类型?您如何在Python数组中指定元素的数据类型?May 03, 2025 am 12:06 AM

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。

什么是Numpy,为什么对于Python中的数值计算很重要?什么是Numpy,为什么对于Python中的数值计算很重要?May 03, 2025 am 12:03 AM

NumPyisessentialfornumericalcomputinginPythonduetoitsspeed,memoryefficiency,andcomprehensivemathematicalfunctions.1)It'sfastbecauseitperformsoperationsinC.2)NumPyarraysaremorememory-efficientthanPythonlists.3)Itoffersawiderangeofmathematicaloperation

讨论'连续内存分配”的概念及其对数组的重要性。讨论'连续内存分配”的概念及其对数组的重要性。May 03, 2025 am 12:01 AM

Contiguousmemoryallocationiscrucialforarraysbecauseitallowsforefficientandfastelementaccess.1)Itenablesconstanttimeaccess,O(1),duetodirectaddresscalculation.2)Itimprovescacheefficiencybyallowingmultipleelementfetchespercacheline.3)Itsimplifiesmemorym

您如何切成python列表?您如何切成python列表?May 02, 2025 am 12:14 AM

SlicingaPythonlistisdoneusingthesyntaxlist[start:stop:step].Here'showitworks:1)Startistheindexofthefirstelementtoinclude.2)Stopistheindexofthefirstelementtoexclude.3)Stepistheincrementbetweenelements.It'susefulforextractingportionsoflistsandcanuseneg

在Numpy阵列上可以执行哪些常见操作?在Numpy阵列上可以执行哪些常见操作?May 02, 2025 am 12:09 AM

numpyallowsforvariousoperationsonArrays:1)basicarithmeticlikeaddition,减法,乘法和division; 2)evationAperationssuchasmatrixmultiplication; 3)element-wiseOperations wiseOperationswithOutexpliitloops; 4)

Python的数据分析中如何使用阵列?Python的数据分析中如何使用阵列?May 02, 2025 am 12:09 AM

Arresinpython,尤其是Throughnumpyandpandas,weessentialFordataAnalysis,offeringSpeedAndeffied.1)NumpyArseNable efflaysenable efficefliceHandlingAtaSetSetSetSetSetSetSetSetSetSetSetsetSetSetSetSetsopplexoperationslikemovingaverages.2)

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版