Skip to content

Commit 31db69e

Browse files
committed
📝 Recommend uv projects by default in docs
1 parent 305e551 commit 31db69e

32 files changed

Lines changed: 128 additions & 1199 deletions

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,19 @@ As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. T
6262

6363
## Installation
6464

65-
Make sure you create a [virtual environment](https://sqlmodel.tiangolo.com/virtual-environments/), activate it, and then install SQLModel, for example with:
65+
First, [install `uv`](https://docs.astral.sh/uv/getting-started/installation/), and then add SQLModel to your project:
6666

6767
<div class="termy">
6868

6969
```console
70-
$ pip install sqlmodel
70+
$ uv add sqlmodel
7171
---> 100%
72-
Successfully installed sqlmodel
7372
```
7473

7574
</div>
7675

76+
If you prefer to use `pip`, install `sqlmodel` inside a virtual environment. See the [installation guide](install.md) for the alternative steps.
77+
7778
## Example
7879

7980
For an introduction to databases, SQL, and everything else, see the [SQLModel documentation](https://sqlmodel.tiangolo.com/databases/).

docs/advanced/decimal.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Now if you run this, instead of printing the unexpected number `3.30000000000000
8282
<div class="termy">
8383

8484
```console
85-
$ python app.py
85+
$ uv run python app.py
8686

8787
// Some boilerplate and previous output omitted 😉
8888

docs/advanced/uuid.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ If you run the program, you will see the **UUID** generated in the Python code,
127127
<div class="termy">
128128

129129
```console
130-
$ python app.py
130+
$ uv run python app.py
131131

132132
// Some boilerplate and previous output omitted 😉
133133

docs/environment-variables.md

Lines changed: 4 additions & 295 deletions
Original file line numberDiff line numberDiff line change
@@ -1,300 +1,9 @@
11
# Environment Variables
22

3-
Before we jump into code, let's cover a bit some of the **basics** that we'll need to understand how to work with Python (and programming) in general. Let's check a bit about **environment variables**.
3+
An **environment variable** (also known as an **env var**) is a value that lives outside of your Python code, in the operating system, and can be read by your application and other programs.
44

5-
/// tip
5+
Applications commonly use environment variables for configuration such as database URLs, credentials, and secret keys.
66

7-
If you already know what "environment variables" are and how to use them, feel free to skip this.
7+
## Learn More { #learn-more }
88

9-
///
10-
11-
An environment variable (also known as "**env var**") is a variable that lives **outside** of the Python code, in the **operating system**, and could be read by your Python code (or by other programs as well).
12-
13-
Environment variables could be useful for handling application **settings**, as part of the **installation** of Python, etc.
14-
15-
## Create and Use Env Vars
16-
17-
You can **create** and use environment variables in the **shell (terminal)**, without needing Python:
18-
19-
//// tab | Linux, macOS, Windows Bash
20-
21-
<div class="termy">
22-
23-
```console
24-
// You could create an env var MY_NAME with
25-
$ export MY_NAME="Wade Wilson"
26-
27-
// Then you could use it with other programs, like
28-
$ echo "Hello $MY_NAME"
29-
30-
Hello Wade Wilson
31-
```
32-
33-
</div>
34-
35-
////
36-
37-
//// tab | Windows PowerShell
38-
39-
<div class="termy">
40-
41-
```console
42-
// Create an env var MY_NAME
43-
$ $Env:MY_NAME = "Wade Wilson"
44-
45-
// Use it with other programs, like
46-
$ echo "Hello $Env:MY_NAME"
47-
48-
Hello Wade Wilson
49-
```
50-
51-
</div>
52-
53-
////
54-
55-
## Read env vars in Python
56-
57-
You could also create environment variables **outside** of Python, in the terminal (or with any other method), and then **read them in Python**.
58-
59-
For example you could have a file `main.py` with:
60-
61-
```Python hl_lines="3"
62-
import os
63-
64-
name = os.getenv("MY_NAME", "World")
65-
print(f"Hello {name} from Python")
66-
```
67-
68-
/// tip
69-
70-
The second argument to [`os.getenv()`](https://docs.python.org/3.8/library/os.html#os.getenv) is the default value to return.
71-
72-
If not provided, it's `None` by default, here we provide `"World"` as the default value to use.
73-
74-
///
75-
76-
Then you could call that Python program:
77-
78-
//// tab | Linux, macOS, Windows Bash
79-
80-
<div class="termy">
81-
82-
```console
83-
// Here we don't set the env var yet
84-
$ python main.py
85-
86-
// As we didn't set the env var, we get the default value
87-
88-
Hello World from Python
89-
90-
// But if we create an environment variable first
91-
$ export MY_NAME="Wade Wilson"
92-
93-
// And then call the program again
94-
$ python main.py
95-
96-
// Now it can read the environment variable
97-
98-
Hello Wade Wilson from Python
99-
```
100-
101-
</div>
102-
103-
////
104-
105-
//// tab | Windows PowerShell
106-
107-
<div class="termy">
108-
109-
```console
110-
// Here we don't set the env var yet
111-
$ python main.py
112-
113-
// As we didn't set the env var, we get the default value
114-
115-
Hello World from Python
116-
117-
// But if we create an environment variable first
118-
$ $Env:MY_NAME = "Wade Wilson"
119-
120-
// And then call the program again
121-
$ python main.py
122-
123-
// Now it can read the environment variable
124-
125-
Hello Wade Wilson from Python
126-
```
127-
128-
</div>
129-
130-
////
131-
132-
As environment variables can be set outside of the code, but can be read by the code, and don't have to be stored (committed to `git`) with the rest of the files, it's common to use them for configurations or **settings**.
133-
134-
You can also create an environment variable only for a **specific program invocation**, that is only available to that program, and only for its duration.
135-
136-
To do that, create it right before the program itself, on the same line:
137-
138-
<div class="termy">
139-
140-
```console
141-
// Create an env var MY_NAME in line for this program call
142-
$ MY_NAME="Wade Wilson" python main.py
143-
144-
// Now it can read the environment variable
145-
146-
Hello Wade Wilson from Python
147-
148-
// The env var no longer exists afterwards
149-
$ python main.py
150-
151-
Hello World from Python
152-
```
153-
154-
</div>
155-
156-
/// tip
157-
158-
You can read more about it at [The Twelve-Factor App: Config](https://12factor.net/config).
159-
160-
///
161-
162-
## Types and Validation
163-
164-
These environment variables can only handle **text strings**, as they are external to Python and have to be compatible with other programs and the rest of the system (and even with different operating systems, as Linux, Windows, macOS).
165-
166-
That means that **any value** read in Python from an environment variable **will be a `str`**, and any conversion to a different type or any validation has to be done in code.
167-
168-
## `PATH` Environment Variable
169-
170-
There is a **special** environment variable called **`PATH`** that is used by the operating systems (Linux, macOS, Windows) to find programs to run.
171-
172-
The value of the variable `PATH` is a long string that is made of directories separated by a colon `:` on Linux and macOS, and by a semicolon `;` on Windows.
173-
174-
For example, the `PATH` environment variable could look like this:
175-
176-
//// tab | Linux, macOS
177-
178-
```plaintext
179-
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
180-
```
181-
182-
This means that the system should look for programs in the directories:
183-
184-
* `/usr/local/bin`
185-
* `/usr/bin`
186-
* `/bin`
187-
* `/usr/sbin`
188-
* `/sbin`
189-
190-
////
191-
192-
//// tab | Windows
193-
194-
```plaintext
195-
C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32
196-
```
197-
198-
This means that the system should look for programs in the directories:
199-
200-
* `C:\Program Files\Python312\Scripts`
201-
* `C:\Program Files\Python312`
202-
* `C:\Windows\System32`
203-
204-
////
205-
206-
When you type a **command** in the terminal, the operating system **looks for** the program in **each of those directories** listed in the `PATH` environment variable.
207-
208-
For example, when you type `python` in the terminal, the operating system looks for a program called `python` in the **first directory** in that list.
209-
210-
If it finds it, then it will **use it**. Otherwise it keeps looking in the **other directories**.
211-
212-
### Installing Python and Updating the `PATH`
213-
214-
When you install Python, you might be asked if you want to update the `PATH` environment variable.
215-
216-
//// tab | Linux, macOS
217-
218-
Let's say you install Python and it ends up in a directory `/opt/custompython/bin`.
219-
220-
If you say yes to update the `PATH` environment variable, then the installer will add `/opt/custompython/bin` to the `PATH` environment variable.
221-
222-
It could look like this:
223-
224-
```plaintext
225-
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/custompython/bin
226-
```
227-
228-
This way, when you type `python` in the terminal, the system will find the Python program in `/opt/custompython/bin` (the last directory) and use that one.
229-
230-
////
231-
232-
//// tab | Windows
233-
234-
Let's say you install Python and it ends up in a directory `C:\opt\custompython\bin`.
235-
236-
If you say yes to update the `PATH` environment variable, then the installer will add `C:\opt\custompython\bin` to the `PATH` environment variable.
237-
238-
```plaintext
239-
C:\Program Files\Python312\Scripts;C:\Program Files\Python312;C:\Windows\System32;C:\opt\custompython\bin
240-
```
241-
242-
This way, when you type `python` in the terminal, the system will find the Python program in `C:\opt\custompython\bin` (the last directory) and use that one.
243-
244-
////
245-
246-
This way, when you type `python` in the terminal, the system will find the Python program in `/opt/custompython/bin` (the last directory) and use that one.
247-
248-
So, if you type:
249-
250-
<div class="termy">
251-
252-
```console
253-
$ python
254-
```
255-
256-
</div>
257-
258-
//// tab | Linux, macOS
259-
260-
The system will **find** the `python` program in `/opt/custompython/bin` and run it.
261-
262-
It would be roughly equivalent to typing:
263-
264-
<div class="termy">
265-
266-
```console
267-
$ /opt/custompython/bin/python
268-
```
269-
270-
</div>
271-
272-
////
273-
274-
//// tab | Windows
275-
276-
The system will **find** the `python` program in `C:\opt\custompython\bin\python` and run it.
277-
278-
It would be roughly equivalent to typing:
279-
280-
<div class="termy">
281-
282-
```console
283-
$ C:\opt\custompython\bin\python
284-
```
285-
286-
</div>
287-
288-
////
289-
290-
This information will be useful when learning about [Virtual Environments](virtual-environments.md).
291-
292-
## Conclusion
293-
294-
With this you should have a basic understanding of what **environment variables** are and how to use them in Python.
295-
296-
You can also read more about them in the [Wikipedia for Environment Variable](https://en.wikipedia.org/wiki/Environment_variable).
297-
298-
In many cases it's not very obvious how environment variables would be useful and applicable right away. But they keep showing up in many different scenarios when you are developing, so it's good to know about them.
299-
300-
For example, you will need this information in the next section, about [Virtual Environments](virtual-environments.md).
9+
Read the [Environment Variables guide](https://tiangolo.com/guides/environment-variables/) for a detailed, cross-platform explanation, including how to create and read environment variables and how the `PATH` environment variable works.

docs/index.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,19 @@ As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. T
8282

8383
## Installation
8484

85-
Make sure you create a [virtual environment](https://sqlmodel.tiangolo.com/virtual-environments/), activate it, and then install SQLModel, for example with:
85+
First, [install `uv`](https://docs.astral.sh/uv/getting-started/installation/), and then add SQLModel to your project:
8686

8787
<div class="termy">
8888

8989
```console
90-
$ pip install sqlmodel
90+
$ uv add sqlmodel
9191
---> 100%
92-
Successfully installed sqlmodel
9392
```
9493

9594
</div>
9695

96+
If you prefer to use `pip`, install `sqlmodel` inside a virtual environment. See the [installation guide](install.md) for the alternative steps.
97+
9798
## Example
9899

99100
For an introduction to databases, SQL, and everything else, see the [SQLModel documentation](https://sqlmodel.tiangolo.com/databases/).

0 commit comments

Comments
 (0)