search
HomeBackend DevelopmentPython TutorialUse uv to manage Python environments

使用 uv 管理 Python 環境

Say goodbye to cumbersome Python environment management! uv is an efficient and convenient tool that can solve Python version management, virtual environment creation, package management, project management and other problems in one stop. It is fast and easy to get started. This article will take Windows PowerShell as an example to demonstrate the use of uv. For other platforms, you can refer to the official documentation for corresponding adjustments.

Install uv

uv does not depend on Python, so it is not recommended to use pip or pipx to install. Windows systems can be installed directly by executing the following command through PowerShell:

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

Or install using a package manager such as scoop:

scoop install uv

Use uv to manage multiple versions of Python

Use the uv python list command to view installable and installed Python versions:

# 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="">
...

Install the latest version:

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

View installation results: The installed version will display the installation path.

# 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
...

Get the Python installation path:

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

Install the specified version:

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

Uninstall Python version (requires specified version):

# 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

View all versions (including all revisions): uv python list --all-versions

Install multiple versions: uv python install 3.10 3.11

Uninstall multiple versions: uv python uninstall 3.10 3.11

Use uv instead of python/pip tools

The Python environment managed by uv cannot be executed directly with the python command, but must be executed through the uv run command. For example:

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

Execution:

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

Specify Python version to execute: uv run --python 3.10 .show_version.py

Execute from standard input: echo 'print("hello world!")' | uv run -

View installed Python version: uv python list --only-installed

Set default Python version (current directory only): uv python pin 3.10 (create .python-version file)

Specify the packages required for execution

If the program requires additional packages, such as cowsay:

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

Specify the package using the --with option:

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

Clear cached virtual environments: uv cache clean

Manage virtual environments

Create a virtual environment: uv venv --python 3.10 (create .venv directory) or specify the directory name: uv venv myenv

Use the specified virtual environment: uv run --python myenv .show_version.py

Delete virtual environment: Delete virtual environment directory

Management Pack

Use the uv pip command to manage packages, which is compatible with the pip command.

Installation package: uv pip install cowsay

View package dependencies: uv pip tree

Uninstall package: uv pip uninstall rich (Dependent packages no longer needed will not be automatically deleted)

Use uv to manage Python projects

uv provides two project management methods: single file project and folder project.

Single file project

Initialize single file project: uv init --script cow3.py --python 3.13 (add metadata in cow3.py file)

Add package: uv add --script cow3.py cowsay rich (modify cow3.py file metadata)

Remove package: uv remove --script cow3.py rich (modify cow3.py file metadata)

Folder Items

Initialize the folder project: uv init myproject (Create the project directory, including .gitignore, .python-version, hello.py, pyproject.toml, README.md)

Execution project: uv run hello.py (Create .venv virtual environment)

Add package: uv add cowsay rich (modify pyproject.toml file)

Update package: uv lock --upgrade-package cowsay or uv lock --upgrade

Remove package: uv remove cowsay

Synchronize project environment with uv.lock files: uv sync

View project package dependencies: uv tree

Use the tool commands provided by the package

Directly execute the package command: uvx cowsay -t 'hello, uv' or uv tool run cowsay -t 'hello, uv'

Specify package execution command: uvx --from httpie http -p=b GET https://flagtech.github.io/flag.txt

Install package command to the system: uv tool install httpie

Update package command: uv tool upgrade httpie

Uninstall package command: uv tool uninstall httpie

uv provides an efficient and convenient Python environment management solution, significantly improving development efficiency. Through the introduction of this article, I believe you have mastered the basic usage of uv and can better manage your Python projects and environments.

The above is the detailed content of Use uv to manage Python environments. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you append elements to a Python list?How do you append elements to a Python list?May 04, 2025 am 12:17 AM

ToappendelementstoaPythonlist,usetheappend()methodforsingleelements,extend()formultipleelements,andinsert()forspecificpositions.1)Useappend()foraddingoneelementattheend.2)Useextend()toaddmultipleelementsefficiently.3)Useinsert()toaddanelementataspeci

How do you create a Python list? Give an example.How do you create a Python list? Give an example.May 04, 2025 am 12:16 AM

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

Discuss real-world use cases where efficient storage and processing of numerical data are critical.Discuss real-world use cases where efficient storage and processing of numerical data are critical.May 04, 2025 am 12:11 AM

In the fields of finance, scientific research, medical care and AI, it is crucial to efficiently store and process numerical data. 1) In finance, using memory mapped files and NumPy libraries can significantly improve data processing speed. 2) In the field of scientific research, HDF5 files are optimized for data storage and retrieval. 3) In medical care, database optimization technologies such as indexing and partitioning improve data query performance. 4) In AI, data sharding and distributed training accelerate model training. System performance and scalability can be significantly improved by choosing the right tools and technologies and weighing trade-offs between storage and processing speeds.

How do you create a Python array? Give an example.How do you create a Python array? Give an example.May 04, 2025 am 12:10 AM

Pythonarraysarecreatedusingthearraymodule,notbuilt-inlikelists.1)Importthearraymodule.2)Specifythetypecode,e.g.,'i'forintegers.3)Initializewithvalues.Arraysofferbettermemoryefficiencyforhomogeneousdatabutlessflexibilitythanlists.

What are some alternatives to using a shebang line to specify the Python interpreter?What are some alternatives to using a shebang line to specify the Python interpreter?May 04, 2025 am 12:07 AM

In addition to the shebang line, there are many ways to specify a Python interpreter: 1. Use python commands directly from the command line; 2. Use batch files or shell scripts; 3. Use build tools such as Make or CMake; 4. Use task runners such as Invoke. Each method has its advantages and disadvantages, and it is important to choose the method that suits the needs of the project.

How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?How does the choice between lists and arrays impact the overall performance of a Python application dealing with large datasets?May 03, 2025 am 12:11 AM

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

Explain how memory is allocated for lists versus arrays in Python.Explain how memory is allocated for lists versus arrays in Python.May 03, 2025 am 12:10 AM

InPython,listsusedynamicmemoryallocationwithover-allocation,whileNumPyarraysallocatefixedmemory.1)Listsallocatemorememorythanneededinitially,resizingwhennecessary.2)NumPyarraysallocateexactmemoryforelements,offeringpredictableusagebutlessflexibility.

How do you specify the data type of elements in a Python array?How do you specify the data type of elements in a Python array?May 03, 2025 am 12:06 AM

InPython, YouCansSpectHedatatYPeyFeLeMeReModelerErnSpAnT.1) UsenPyNeRnRump.1) UsenPyNeRp.DLOATP.PLOATM64, Formor PrecisconTrolatatypes.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)