Skip to content

Commit 3e3dc3c

Browse files
authored
Native uv docs (rasbt#530)
* Replace pip by more modern uv * uv tests * Native uv docs * resolve merge conflicts * resolve merge conflicts
1 parent e9c4dac commit 3e3dc3c

File tree

4 files changed

+214
-3
lines changed

4 files changed

+214
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ celerybeat.pid
244244
# Environments
245245
.env
246246
.venv
247+
.python-version
248+
uv.lock
247249
env/
248250
venv/
249251
ENV/

pyproject.toml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
[project]
2+
name = "llms-from-scratch"
3+
version = "0.1.0"
4+
description = "mplement a ChatGPT-like LLM in PyTorch from scratch, step by step"
5+
readme = "README.md"
6+
requires-python = ">=3.10"
7+
dependencies = [
8+
"torch>=2.3.0",
9+
"jupyterlab>=4.0",
10+
"tiktoken>=0.5.1",
11+
"matplotlib>=3.7.1",
12+
"tensorflow>=2.18.0",
13+
"tqdm>=4.66.1",
14+
"numpy>=1.26,<2.1",
15+
"pandas>=2.2.1",
16+
"psutil>=5.9.5",
17+
"packaging>=24.2",
18+
]
19+
20+
[tool.setuptools.packages]
21+
find = {}
22+
23+
[tool.uv.sources]
24+
llms-from-scratch = { workspace = true }
25+
26+
[dependency-groups]
27+
dev = [
28+
"llms-from-scratch",
29+
]

setup/01_optional-python-setup-preferences/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,37 @@ I have been a long-time user of [Conda](https://anaconda.org/anaconda/conda) and
88

99
I recommend starting with *Option 1: Using uv* as it is the more modern approach in 2025. If you encounter problems with *Option 1*, consider *Option 2: Using Conda*.
1010

11+
In this tutorial, I am using a computer running macOS, but this workflow is similar for Linux machines and may work for other operating systems as well.
12+
1113

1214
&nbsp;
1315
# Option 1: Using uv
1416

15-
&nbsp;
17+
This section guides you through the Python setup and package installation procedure using `uv` via its `uv pip` interface. The `uv pip` interface may feel more familiar to most Python users who have used pip before than the native `uv` commands.
1618

17-
This section guides you through the Python setup and package installation procedure using `uv`.
19+
&nbsp;
20+
> [!NOTE]
21+
> There are alternative ways to install Python and use `uv`. For example, you can install Python directly via `uv` and use `uv add` instead of `uv pip install` for faster package management.
22+
>
23+
> If you prefer the native `uv` commands, refer to the [./native-uv.md tutorial](./native-uv.md). I also recommend checking the official [`uv` documentation](https://docs.astral.sh/uv/).
24+
>
25+
> While `uv add` offers speed advantages, I find `uv pip` slightly more user-friendly, making it a good starting point for beginners. However, if you're new to Python package management, the native `uv` interface is also a great way to learn.
1826
19-
In this tutorial, I am using a computer running macOS, but this workflow is similar for Linux machines and may work for other operating systems as well.
2027

2128

2229

30+
&nbsp;
2331
## 1. Install Python (if not installed)
2432

33+
2534
First, check if you have a modern version of Python installed (I recommend 3.10 or newer) by executing the following code in the terminal:
2635

2736
```bash
2837
python --version
2938
```
3039
If it returns 3.10 or newer, no further action is required.
3140

41+
&nbsp;
3242
> [!NOTE]
3343
> I recommend installing a Python version that is at least 2 versions older than the most recent release to ensure PyTorch compatibility. For example, if the most recent version is Python 3.13, I recommend installing version 3.10 or 3.11.
3444
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# Native uv Python and package management
2+
3+
This tutorial is an alternative to *Option 1: Using uv* in the [README.md](./README.md) document for those who prefer `uv`'s native commands over the `uv pip` interface. While `uv pip` is faster than pure `pip`, `uv`'s native interface is even faster than `uv pip` as it has less overhead and doesn't have to handle legacy support for PyPy package dependency management.
4+
5+
Otherwise, similar to *Option 1: Using uv* in the [README.md](./README.md) , this section guides you through the Python setup and package installation procedure using `uv`.
6+
7+
In this tutorial, I am using a computer running macOS, but this workflow is similar for Linux machines and may work for other operating systems as well.
8+
9+
10+
&nbsp;
11+
## 1. Install uv
12+
13+
Uv can be installed as follows, depending on your operating system.
14+
15+
&nbsp;
16+
**macOS and Linux**
17+
18+
```bash
19+
curl -LsSf https://astral.sh/uv/install.sh | sh
20+
```
21+
22+
or
23+
24+
```bash
25+
wget -qO- https://astral.sh/uv/install.sh | sh
26+
```
27+
28+
&nbsp;
29+
**Windows**
30+
31+
```bash
32+
powershell -c "irm https://astral.sh/uv/install.ps1 | more"
33+
```
34+
35+
&nbsp;
36+
37+
> [!NOTE]
38+
> For more installation options, please refer to the official [uv documentation](https://docs.astral.sh/uv/getting-started/installation/#standalone-installer).
39+
40+
&nbsp;
41+
## 2. Install Python
42+
43+
You can install Python using uv:
44+
45+
```bash
46+
uv python install 3.10
47+
```
48+
49+
&nbsp;
50+
> [!NOTE]
51+
> I recommend installing a Python version that is at least 2 versions older than the most recent release to ensure PyTorch compatibility. For example, if the most recent version is Python 3.13, I recommend installing version 3.10 or 3.11. You can find out the most recent Python version by visiting [python.org](https://www.python.org/downloads/).
52+
53+
&nbsp;
54+
## 3. Install Python packages and dependencies
55+
56+
To install all required packages from a `pyproject.toml` file (such as the one located at the top level of this GitHub repository), run the following command, assuming the file is in the same directory as your terminal session:
57+
58+
```bash
59+
uv add . --dev
60+
```
61+
62+
<img src="https://sebastianraschka.com/images/LLMs-from-scratch-images/setup/uv-setup/uv-add.png?1" width="700" height="auto" alt="Uv install">
63+
64+
Note that the `uv add` command above will create a separate virtual environment via the `.venv` subfolder.
65+
66+
You can install new packages, that are not specified in the `pyproject.toml` via `uv add`, for example:
67+
68+
```bash
69+
uv add packaging
70+
```
71+
72+
&nbsp;
73+
## Optional: Manage virtual environments manually
74+
75+
Alternatively, you can still install the dependencies directly from the repository using `uv pip install`. Note that this requires creating and activating the virtual environment manually:
76+
77+
&nbsp;
78+
**1. Create a new virtual environment**
79+
80+
Run the following command to manually create a new virtual environment, which will be saved via a new `.venv` subfolder:
81+
82+
```bash
83+
uv venv --python=python3.10
84+
```
85+
86+
&nbsp;
87+
**2. Activate virtual environment**
88+
89+
Next, we need to activate this new virtual environment.
90+
91+
On macOS/Linux:
92+
93+
```bash
94+
source .venv/bin/activate
95+
```
96+
97+
On Windows (PowerShell):
98+
99+
```bash
100+
.venv\Scripts\activate
101+
```
102+
103+
&nbsp;
104+
**3. Install dependencies**
105+
106+
Finally, we can install dependencies from a remote location using the `uv pip` interface:
107+
108+
```bash
109+
uv pip install -U -r https://raw.githubusercontent.com/rasbt/LLMs-from-scratch/refs/heads/main/requirements.txt
110+
```
111+
112+
113+
114+
&nbsp;
115+
## 4. Run Python code
116+
117+
&nbsp;
118+
**Finalizing the setup**
119+
120+
Your environment should now be ready to run the code in the repository.
121+
122+
Optionally, you can run an environment check by executing the `python_environment_check.py` script in this repository:
123+
124+
```bash
125+
uv run python setup/02_installing-python-libraries/python_environment_check.py
126+
```
127+
128+
129+
130+
<img src="https://sebastianraschka.com/images/LLMs-from-scratch-images/setup/uv-setup/uv-run-check.png?1" width="700" height="auto" alt="Uv install">
131+
132+
133+
134+
Or, if you don't want to type `uv run python` ever time you execute code, manually activate the virtual environment first.
135+
136+
On macOS/Linux:
137+
138+
```bash
139+
source .venv/bin/activate
140+
```
141+
142+
On Windows (PowerShell):
143+
144+
```bash
145+
.venv\Scripts\activate
146+
```
147+
148+
Then, run:
149+
150+
151+
```bash
152+
python setup/02_installing-python-libraries/python_environment_check.py
153+
```
154+
155+
&nbsp;
156+
**Launching JupyterLab**
157+
158+
You can launch a JupyterLab instance via:
159+
160+
```bash
161+
uv run jupyter lab
162+
```
163+
164+
Or, if you manually activated the environment as described earlier, you can drop the `uv run` prefix.
165+
166+
&nbsp;
167+
168+
---
169+
170+
Any questions? Please feel free to reach out in the [Discussion Forum](https://github.com/rasbt/LLMs-from-scratch/discussions).

0 commit comments

Comments
 (0)