Mastering Python Project Management with uv PartIt&#s Time to Ditch Poetry

Barbara Streisand
Release: 2024-09-30 06:16:02
Original
866 people have browsed it

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])
Copy after login

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

$ uv add --script app.py 'requests<3' 'rich'
Copy after login

自動元數據生成

這會將元資料加入腳本檔案:

# /// script
# dependencies = [
#   "requests<3",
#   "rich",
# ]
# ///
Copy after login

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

$ uv run app.py
Copy after login

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


2. 建立和管理虛擬環境

虛擬環境入門

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

$ uv venv
Copy after login

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

$ uv venv my_env --python 3.9
Copy after login

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

何時使用 uv add 與 uv pip install

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

    $ uv add fastapi
    
    Copy after login

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

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

    $ uv pip install requests
    
    Copy after login

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


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

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

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

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

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


4. 管理Python版本

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

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

$ uv python install 3.8 3.9 3.10
Copy after login

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

$ uv run --python 3.10 app.py
Copy after login

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

$ uv python pin 3.9
Copy after login

不再需要雜耍 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
Copy after login

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"
Copy after login

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
    
    Copy after login
  • Locally within a Project:

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

    $ uvx black my_code.py
    
    Copy after login

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!

source:dev.to
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!