Skip to content

Commit 2c80535

Browse files
committed
📝 Add docs for virtual environments and environment variables
1 parent 45f49d5 commit 2c80535

File tree

11 files changed

+1197
-204
lines changed

11 files changed

+1197
-204
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
*.pyc
2-
env*
32
.mypy_cache
43
.vscode
54
.idea
@@ -12,3 +11,4 @@ coverage.xml
1211
site
1312
*.db
1413
.cache
14+
.venv*

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. T
6565

6666
## Installation
6767

68+
Make sure you create a <a href="https://sqlmodel.tiangolo.com/virtual-environments/" class="external-link" target="_blank">virtual environment</a>, activate it, and then install SQLModel, for example with:
69+
6870
<div class="termy">
6971

7072
```console

docs/databases.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
Are you a seasoned developer and already know everything about databases? 🤓
66

7-
Then you can skip to the [Tutorial - User Guide: First Steps](tutorial/index.md){.internal-link target=_blank} right away.
7+
Then you can skip to the next sections right away.
88

99
///
1010

docs/environment-variables.md

Lines changed: 300 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,300 @@
1+
# Environment Variables
2+
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**.
4+
5+
/// tip
6+
7+
If you already know what "environment variables" are and how to use them, feel free to skip this.
8+
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 <a href="https://docs.python.org/3.8/library/os.html#os.getenv" class="external-link" target="_blank">`os.getenv()`</a> 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 <a href="https://12factor.net/config" class="external-link" target="_blank">The Twelve-Factor App: Config</a>.
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){.internal-link target=_blank}.
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 <a href="https://en.wikipedia.org/wiki/Environment_variable" class="external-link" target="_blank">Wikipedia for Environment Variable</a>.
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).

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ As **SQLModel** is based on **Pydantic** and **SQLAlchemy**, it requires them. T
7878

7979
## Installation
8080

81+
Make sure you create a <a href="https://sqlmodel.tiangolo.com/virtual-environments/" class="external-link" target="_blank">virtual environment</a>, activate it, and then install SQLModel, for example with:
82+
8183
<div class="termy">
8284

8385
```console

docs/install.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Install **SQLModel**
2+
3+
Create a project directory, create a [virtual environment](virtual-environments.md){.internal-link target=_blank}, activate it, and then install **SQLModel**, for example with:
4+
5+
<div class="termy">
6+
7+
```console
8+
$ pip install sqlmodel
9+
---> 100%
10+
Successfully installed sqlmodel pydantic sqlalchemy
11+
```
12+
13+
</div>
14+
15+
As **SQLModel** is built on top of <a href="https://www.sqlalchemy.org/" class="external-link" target="_blank">SQLAlchemy</a> and <a href="https://pydantic-docs.helpmanual.io/" class="external-link" target="_blank">Pydantic</a>, when you install `sqlmodel` they will also be automatically installed.
16+
17+
## Install DB Browser for SQLite
18+
19+
Remember that [SQLite is a simple database in a single file](../databases.md#a-single-file-database){.internal-link target=_blank}?
20+
21+
For most of the tutorial I'll use SQLite for the examples.
22+
23+
Python has integrated support for SQLite, it is a single file read and processed from Python. And it doesn't need an [External Database Server](../databases.md#a-server-database){.internal-link target=_blank}, so it will be perfect for learning.
24+
25+
In fact, SQLite is perfectly capable of handling quite big applications. At some point you might want to migrate to a server-based database like <a href="https://www.postgresql.org/" class="external-link" target="_blank">PostgreSQL</a> (which is also free). But for now we'll stick to SQLite.
26+
27+
Through the tutorial I will show you SQL fragments, and Python examples. And I hope (and expect 🧐) you to actually run them, and verify that the database is working as expected and showing you the same data.
28+
29+
To be able to explore the SQLite file yourself, independent of Python code (and probably at the same time), I recommend you use <a href="https://sqlitebrowser.org/" class="external-link" target="_blank">DB Browser for SQLite</a>.
30+
31+
It's a great and simple program to interact with SQLite databases (SQLite files) in a nice user interface.
32+
33+
<img src="https://sqlitebrowser.org/images/screenshot.png">
34+
35+
Go ahead and <a href="https://sqlitebrowser.org/" class="external-link" target="_blank">Install DB Browser for SQLite</a>, it's free.
36+
37+
## Next Steps
38+
39+
Okay, let's get going! On the next section we'll start the [Tutorial - User Guide](tutorial/index.md). 🚀

docs/tutorial/fastapi/simple-hero-api.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ FastAPI is the framework to create the **web API**.
1010

1111
But we also need another type of program to run it, it is called a "**server**". We will use **Uvicorn** for that. And we will install Uvicorn with its *standard* dependencies.
1212

13-
Make sure you [have a virtual environment activated](../index.md#create-a-python-virtual-environment){.internal-link target=_blank}.
13+
Then install FastAPI.
1414

15-
Then install FastAPI and Uvicorn:
15+
Make sure you create a [virtual environment](../../virtual-environments.md){.internal-link target=_blank}, activate it, and then install them, for example with:
1616

1717
<div class="termy">
1818

1919
```console
20-
$ python -m pip install fastapi "uvicorn[standard]"
20+
$ pip install fastapi "uvicorn[standard]"
2121

2222
---> 100%
2323
```

0 commit comments

Comments
 (0)