January 26, 2025

פייתון - הכלי UV

הקדמה

לכל שפת תכנות יש את יכולות השפה וה-eco-system שלה.
מה שנמצא בתוך השפה הוא בדרך כלל הסטנדרט של השפה,
כל סטנדרט כזה מנוהל בדרך כלל על ידי comittee כזה או אחר.

בפייתון קיימים PEP - Python Enhancement Proposal.
אלו הצעות שחלקן התקבלו לסטנדרט המגדיר כיצד מנגנונים בפייתון עובדים.
כמה מהמפורסמים:

  • PEP 8: Style Guide for Python Code, which defines the conventions for writing clean and readable Python code.
  • PEP 20: The Zen of Python, which outlines the philosophy of Python in 19 guiding principles.
  • PEP 484: Introduced type hints to Python.

מה שמחוץ לסטנדרט זה ה-Eco System.
וזה מגוון הכלים והיכולות שמפותחים לשפה ע”י אנשים, חברות, קבוצות קוד פתוח וכדו’…

כחלק מהסטנדרט יש לנו את הכלי pip שמגיע עם כל התקנת פייתון.
כידוע לכם pip יודע לנהל תלויות בחבילות שאתם צורכים לפייתון.

למשל כדי לשלוח בקשה נוכל להשתמש בחבילה requests ונתקין אותה על ידי:

1
pip install requests

בשביל ליצור התקנות מנוהלות מה שנוכל לעשות זה להכניס את החבילה הזו לקובץ מיוחד בשם requierments.txt.
requierments.txt:

1
requests==2.31.0

פשוט וקל.
אבל…
זה לא כזה סטנדרטי מחוץ לפייתון, כאילו כן, אבל לא באמת.
חוץ מזה, יש חסרונות גדולים לזה,
ההתקנות בדרך כלל הן גלובליות, תלויות באיזו גרסת פייתון, וקשה לנהל גרסאות אוטומטיות ככה, למשל כשצריך לעדכן.

בשביל זה ייצרו גם את ה-venv,
סביבות ווירטואליות לפייתון על מנת להתקין את החבילות בצורה לוקלאית לפרוייקט, דמויי node_modules אם תרצו.
למרות שזו הוספה מעולה, הכלים הקיימים לא מצליחים לאפשר למתכנת לנהל את סביבתו בצורה אופטימלית.
זה אפשרי אבל לא אידיאלי.

בשביל זה ייצרו את הכלי - uv.

UV

כלי מהיר לניהול והתקנה של חבילות קוד - מודולים - לפייתון.
כתוב ב-rust ומחליף את pip ו-pip-tools.

uv מקביל לכמה כלים בשפות שונות:

  • cargo
  • nvm
  • dotnet nuget

כדי ללמוד על הכלי

https://github.com/astral-sh/uv

https://astral.sh/blog/uv

דוגמא

1
git clone https://github.com/Ilya122/UVPyDocLearning.git

https://github.com/Ilya122/UVPyDocLearning

מה יש בפרוייקט:

1
2
3
4
5
6
7
8
9
10
templates - JINJA2 templates
.dockerignore - ignore for docker
.gitignore - ignore for git
.python-version - what py version
README.md - just a readme for repo
app.py - main python file for server
dockerfile - docker installation
multistage.Dockerfile - multistage dockerfile installation
pyproject.toml - our project toml file
uv.lock - the UV lock file for syncing with UV

הדבר הראשון שאפשר לעשות הוא בתוך התיקייה:

1
uv init

מה שמאתחל פרוייקט בפייתון.

ניתן להשתמש ב:

1
uv venv

כדי לאתחל גם סביבה ווירטואלית
ואחר כך זה נהיה הרבה יותר קל, כדי להוסיף חבילה נעשה

1
uv add <package>

וכדי למחוק

1
uv remove <package>

שימוש ב-dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Use a Python image with uv pre-installed
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim

# Install the project into `/app`
WORKDIR /app

# Enable bytecode compilation
ENV UV_COMPILE_BYTECODE=1

# Copy from the cache instead of linking since it's a mounted volume
ENV UV_LINK_MODE=copy

# Install the project's dependencies using the lockfile and settings
RUN --mount=type=cache,target=/root/.cache/uv \
--mount=type=bind,source=uv.lock,target=uv.lock \
--mount=type=bind,source=pyproject.toml,target=pyproject.toml \
uv sync --frozen --no-install-project --no-dev

# Then, add the rest of the project source code and install it
# Installing separately from its dependencies allows optimal layer caching
ADD . /app
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --frozen --no-dev

# Place executables in the environment at the front of the path
ENV PATH="/app/.venv/bin:$PATH"

# Reset the entrypoint, don't invoke `uv`
ENTRYPOINT []

CMD ["gunicorn","-b :8080", "app:wsgi_application"]

נוכל לראות כל מיני פקודות מוזרות שאספתי במקרה מהאתר של uv.
הבעיה עם זה, זו הוספה של כלי לא סטנדרטי שצריך ללמוד לעבוד איתו,
כמובן הם מציעים שכבה של פייתון עם הכלי מותקן, אפשר לפצל את זה ולהתקין את uv לבד.
בכל מקרה לא רואים פה משהו יותר מדי מסובך, רק כמה sync כדי לעדכן את הסביבה.
הפרוייקט משתמש ב-MicroPie שזו חבילה פשוטה לבניית שרת.
אנחנו משתמשים פה ב-gunicorn כדי להריץ אפליקציית wsgi.

פקודות נוספות

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Usage: uv [OPTIONS] <COMMAND>

Commands:
run Run a command or script
init Create a new project
add Add dependencies to the project
remove Remove dependencies from the project
sync Update the project's environment
lock Update the project's lockfile
export Export the project's lockfile to an alternate format
tree Display the project's dependency tree
tool Run and install commands provided by Python packages
python Manage Python versions and installations
pip Manage Python packages with a pip-compatible interface
venv Create a virtual environment
build Build Python packages into source distributions and wheels
publish Upload distributions to an index
cache Manage uv's cache
self Manage the uv executable
version Display uv's version
help Display documentation for a command

tree

עבור הפרוייקט נראה אילו תלויות אנחנו צורכים .

1
2
3
4
5
6
7
8
uv tree
Resolved 6 packages in 1ms
testserver v0.1.0
├── gunicorn v23.0.0
│ └── packaging v24.2
└── micropie v0.5
└── jinja2 v3.1.5
└── markupsafe v3.0.2

python

מאפשר להוריד התקנות של פייתון

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.13.0-windows-x86_64-none C:\...\Python\Python313\python.exe
cpython-3.12.8-windows-x86_64-none <download available>
cpython-3.11.11-windows-x86_64-none <download available>
cpython-3.10.16-windows-x86_64-none <download available>
cpython-3.9.21-windows-x86_64-none <download available>
cpython-3.9.13-windows-x86_64-none C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\python.exe
cpython-3.8.20-windows-x86_64-none <download available>
cpython-3.7.9-windows-x86_64-none <download available>
pypy-3.10.14-windows-x86_64-none <download available>
pypy-3.9.19-windows-x86_64-none <download available>
pypy-3.8.16-windows-x86_64-none <download available>
pypy-3.7.13-windows-x86_64-none <download available>

ההמשך

כמובן יש הרבה מה לכתוב והרבה מה להסביר,
תורידו ותתחילו לעבוד עם הכלי כדי להתרשם בעצמכם.

זוהי התקדמות טובה אולם הוספנו עוד שכבה, ולכן רמת הסיבוכיות נהייתה גבוה יותר מסתם לכתוב קובץ סקריפט ולהריץ אותו בעזרת py.
חשבו טוב למה אתם צריכים את זה ואיך תשתמשו בזה.
בכל אינטגרציה רצוי גם ללמד את הצוותים על הכלי ולא להשאיר את הכל באויר.

בהצלחה!

על הפוסט

הפוסט נכתב על ידי Ilya, רישיון על ידי CC BY-NC-ND 4.0.

שתפו את הפוסט

Email Facebook Linkedin Print

קנו לי קפה

#Software#Tools#Python