search
HomeBackend DevelopmentPython TutorialMastering Python Project Management with uv PartIt&#s Time to Ditch Poetry

Mastering Python Project Management with uv PartIt

您是否厭倦了為了控制 Python 環境和依賴項而使用 pip、virtualenv、conda、poetry 和 pyenv 等多個工具?你並不孤單!管理 Python 專案可能會讓人感到頭疼,尤其是在需要處理各種不同的套件管理器和工具的情況下。

輸入 uv通用 Virtualenv。將其視為一站式套件管理器,旨在簡化和加速您的 Python 開發流程。


一些背景故事

uv 從另一個現代包裝管理器 Rye 汲取靈感,統一了 pip、pip-tools、pyenv、virtualenv 和 Poetry 的最佳功能。 uv 使用 Rust 構建,不僅速度快,而且效率高,簡化了從管理依賴項到創建虛擬環境的一切。

紫外線的目的

簡而言之,uv 就是整合。當您可以獲得一種統一的體驗時,為什麼要在多種工具之間切換?它旨在消除 Python 開發中的摩擦,為您提供更一致、更快速的專案管理方式。而且速度還很快!這為動態管理開啟了新的大門


1. 具有內嵌腳本元資料的可移植程式碼

讓我們談談依賴關係

uv 最令人興奮的功能之一是能夠直接在 Python 腳本中新增依賴項。想像你有一個像這樣的簡單腳本:

# app.py
import requests
from rich.pretty import pprint

response = requests.get("https://peps.python.org/api/peps.json")
data = response.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])

執行此腳本通常意味著設定虛擬環境並手動安裝依賴項。使用 uv,您可以將所有依賴項直接嵌入到腳本中,使其自包含可共享:

$ uv add --script app.py 'requests



<h3>
  
  
  自動元數據生成
</h3>

<p>這會將元資料加入腳本檔案:<br>
</p>

<pre class="brush:php;toolbar:false"># /// script
# dependencies = [
#   "requests



<p>就是這樣!您可以與其他人共享此文件,他們只需運行:<br>
</p>

<pre class="brush:php;toolbar:false">$ uv run app.py

瞧——無需外部設定!這一切都歸功於 uv 的速度和效率。


2. 建立和管理虛擬環境

虛擬環境入門

預設情況下,uv 要求在虛擬環境中安裝軟體包,以保持系統乾淨並避免不同項目之間的衝突。使用 uv 建立虛擬環境很簡單:

$ uv venv

這將建立一個包含隔離環境的 .venv 目錄。如果你想指定自訂目錄或Python版本,可以這樣做:

$ uv venv my_env --python 3.9

環境已準備好使用,uv 會自動偵測您的所有命令,例如安裝套件或執行腳本。

何時使用 uv add 與 uv pip install

  • 使用 uv add:當您想要在專案的 pyproject.toml 檔案中新增依賴項。當您正在開發專案並希望追蹤所有依賴項以使專案易於共享和複製時,這是最好的選擇。我們將在下一篇文章中介紹這一點,敬請期待!

    $ uv add fastapi
    

    這將更新您的 pyproject.toml 並將版本鎖定在 uv.lock 中。

  • 使用 uv pip install:當您想要安裝套件以快速使用而不修改專案檔案時,或者對於不需要在 pyproject.toml 中追蹤它們的全域工具時。將 uv pip 視為即時的一次性安裝。

    $ uv pip install requests
    

選擇正確的命令可確保您的專案得到正確管理並且易於共享或部署。


3. 鎖定版本以實現可重複性

您的程式碼是否曾因更新而被破壞過?

我們都經歷過這樣的情況——你的程式碼今天可以運行,但明天就會因為套件更新而崩潰。使用 uv,您可以透過鎖定軟體包版本來防止這種情況,以確保一致性和可重複性:

[tool.uv]
exclude-newer = "2023-10-16T00:00:00Z"

這樣,即使您的依賴項出現新版本,您的專案也保持穩定。非常適合無法承受意外的長期專案!


4. 管理Python版本

不同的項目,不同的Python版本?沒問題!

許多開發人員必須處理需要不同 Python 版本的多個專案。 uv 讓切換版本變得如此簡單:

$ uv python install 3.8 3.9 3.10

安裝版本後,它們之間的切換是無縫的:

$ uv run --python 3.10 app.py

如果您想要鎖定專案的特定版本:

$ uv python pin 3.9

不再需要雜耍 pyenv 指令 - uv 會為您處理所有繁重的工作。


5. Say Goodbye to pip Hassles

It's pip—but Faster and Better

uv provides a pip-like experience but with turbocharged performance. Installing packages is straightforward:

$ uv pip install flask

Need to add optional dependencies or install directly from a GitHub repo? No sweat:

$ uv pip install 'torch>=1.10.0' "git+https://github.com/astral-sh/ruff"

No more waiting around for slow installations—uv gets the job done fast and effectively.


6. Manage CLI Tools Globally and Easily

From black to ruff, Get Your Tools Hassle-Free

Whether you're linting code or formatting files, uv makes installing CLI tools easy:

  • Globally:

    $ uv tool install ruff
    
  • Locally within a Project:

    $ uv add ruff
    
  • Run Ephemeral Commands without Installing Globally:

    $ uvx black my_code.py
    

Say goodbye to package conflicts and environment pollution—just run your tools whenever and wherever you need them.


If you're looking to supercharge your Python development and want to stop wrestling with multiple tools, uv is your answer. With its streamlined commands, reproducible environments, and efficient package management, uv makes Python development a pleasure rather than a chore.

Ready to take uv for a spin? ? Start today and experience a better way to manage your Python projects.


Stay tuned for Part 2, where we'll dive deeper into advanced features like leveraging pyproject.toml, handling global vs. local tool installations, and how uv can be your best friend when managing complex environments.

Happy coding! ?✨

For more details and full documentation, check out uv documentation.

The above is the detailed content of Mastering Python Project Management with uv PartIt&#s Time to Ditch Poetry. 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
Reaching Your Python Goals: The Power of 2 Hours DailyReaching Your Python Goals: The Power of 2 Hours DailyApr 20, 2025 am 12:21 AM

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Maximizing 2 Hours: Effective Python Learning StrategiesMaximizing 2 Hours: Effective Python Learning StrategiesApr 20, 2025 am 12:20 AM

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Choosing Between Python and C  : The Right Language for YouChoosing Between Python and C : The Right Language for YouApr 20, 2025 am 12:20 AM

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python vs. C  : A Comparative Analysis of Programming LanguagesPython vs. C : A Comparative Analysis of Programming LanguagesApr 20, 2025 am 12:14 AM

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

2 Hours a Day: The Potential of Python Learning2 Hours a Day: The Potential of Python LearningApr 20, 2025 am 12:14 AM

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

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

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.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft