Home > Backend Development > Python Tutorial > Use uv to manage Python environments

Use uv to manage Python environments

DDD
Release: 2025-01-08 18:16:42
Original
243 people have browsed it

使用 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:

<code class="language-powershell">powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"</code>
Copy after login

Or install using a package manager such as scoop:

<code class="language-bash">scoop install uv</code>
Copy after login

Use uv to manage multiple versions of Python

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

<code class="language-bash"># 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="">
...</code>
Copy after login

Install the latest version:

<code class="language-bash"># uv python install
Installed Python 3.13.1 in 5.89s
+ cpython-3.13.1-windows-x86_64-none</code>
Copy after login

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

<code class="language-bash"># 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
...</code>
Copy after login

Get the Python installation path:

<code class="language-bash"># uv python dir
C:\Users\meebo\AppData\Roaming\uv\python</code>
Copy after login

Install the specified version:

<code class="language-bash"># uv python install 3.10
Installed Python 3.10.16 in 9.78s
+ cpython-3.10.16-windows-x86_64-none</code>
Copy after login

Uninstall Python version (requires specified version):

<code class="language-bash"># 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</code>
Copy after login

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:

<code class="language-python"># cat .\show_version.py
import sys
print(sys.version)</code>
Copy after login

Execution:

<code class="language-bash"># uv run .\show_version.py
3.13.1 (main, Dec 19 2024, 14:38:48) [MSC v.1942 64 bit (AMD64)]</code>
Copy after login

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:

<code class="language-python"># cat .\cow.py
from cowsay import cow
cow('hello, world')</code>
Copy after login

Specify the package using the --with option:

<code class="language-bash"># uv run --with cowsay .\cow.py
Installed 1 package in 13ms
...</code>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template