-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuv.txt
More file actions
166 lines (132 loc) · 4.11 KB
/
uv.txt
File metadata and controls
166 lines (132 loc) · 4.11 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# example - how to use "uv" package manager
mkdir myproject
cd myproject
vi requirements.txt
# -------------------------------------
notebook
jupyterlab
ipywidgets
numpy
pandas
scikit-learn
matplotlib
polars[all]
ipython
arrow
pyarrow
boto3
# -------------------------------------
brew install uv
which uv
# -------------------------------------
# create virtual environment - and activate it
uv venv --python=(which python3.13)
source .venv/bin/activate.fish
# -------------------------------------
python --version # verify that it is 3.13
uv pip install -r requirements.txt
# -------------------------------------
To schedule script via cron,
create a wrapper run1.bash
#!/bin/bash
VENV_PYTHON="/Users/myuser/pyth/.venv/bin/python"
MYDIR="/Users/myuser/pyth"
MYSCRIPT="$MYDIR/myscript.py"
cd $MYDIR
$VENV_PYTHON $MYSCRIPT
Then invoke it in crontab:
* * * * * bash /Users/myuser/pyth/run1.bash >> /Users/myuser/pyth/log.log 2>&1
# -------------------------------------
converting requirements.txt to pyproject.toml and back
# run commands at the root of your project
# method 1
uv init --bare # create minimal pyproject.toml
uv add pandas # add a module
uv add -r requirements.txt # add from requirements.txt into pyproject.toml
uv remove pandas
uv add --upgrade pandas
# method 2
uvx migrate-to-uv
uv lock # create a lock file
# -------------------------------------
cat pyproject.toml
[project]
name = "pyth"
version = "0.1.0"
requires-python = ">=3.13"
dependencies = [
"arrow>=1.3.0",
"boto3>=1.38.0",
"ipython>=9.1.0",
"ipywidgets>=8.1.6",
"jupyterlab>=4.4.1",
"matplotlib>=3.10.1",
"notebook>=7.4.0",
"numpy>=2.2.5",
"pandas>=2.2.3",
"polars[all]>=1.27.1",
"pyarrow>=19.0.1",
"scikit-learn>=1.6.1",
]
# -------------------------------------
# to create requirements.txt from pyproject.toml:
uv pip compile pyproject.toml -o requirements.txt
# -------------------------------------
"uv sync" - used very often.
It reads pyproject.toml file (error if file not available)
and uses it as a recipe to install/remove/upgrade
packages in virtual environment (under .venv).
Note - if virtual environment doesn't exist - it will create it
Example of usage:
Example 1:
git clone <repository>
cd <project>
uv sync # creates .venv and puts all modules under it
Example 2:
# After editing pyproject.toml
# or pulling changes that modify dependencies
# or when environment gets corrupted or out of sync
uv sync
Common options:
uv sync --dev # Include development dependencies
uv sync --no-dev # Exclude development dependencies
uv sync --frozen # Don't update the lock file, just sync to it
uv sync --upgrade # Allow upgrading dependencies to newer versions
uv sync --group test # Include optional dependency groups
uv sync --all-groups # Include all optional groups
uv sync is similar to "npm install" or "yarn install"
Note - you can (and should) specify which python version to use.
you can do it in command line:
uv sync --python 3.11
uv sync --python 3.12.0
uv sync --python python3.11
uv sync --python /usr/bin/python3.11
or you can do it inside pyproject.toml file:
[project]
requires-python = ">=3.11"
[tool.uv]
python = "3.11"
To show python version in your environment:
uv run python --version
# -------------------------------------
# you can use "uv" to run scripts:
uv run script.py
uv run --with requests --with rich script.py
uv run --project /path/to/project /path/to/script.py
uv run --python 3.11 script.py
# you can specify metadata in your python script
# in special comments at the top, for example:
# /// script
# requires-python = ">=3.12"
# dependencies = [
# ]
# ///
# Your Python code here
# -------------------------------------
# You can then add dependencies to this metadata:
uv add --script example.py requests rich
# You can use "uv" in the shebang line instead of python:
#!/usr/bin/env -S uv run --script
# -------------------------------------
build and publish distributions using "uv build" and "uv publish"
# -------------------------------------