2023-10-27 11:09:16 -04:00
(python-dev-environment)=
2023-10-09 13:39:24 -04:00
# Setting up a Python development environment
2021-12-10 09:48:34 -05:00
2024-01-28 20:44:42 -05:00
In this example you will build a Python web application using the [Flask ](https://palletsprojects.com/p/flask/ ) web framework as an exercise.
2023-10-09 13:42:43 -04:00
To make best use of it you should be familiar with [defining declarative shell environments ](declarative-reproducible-envs ).
2021-12-10 09:48:34 -05:00
2023-10-09 13:39:24 -04:00
Create a new file called `myapp.py` and add the following code:
2021-12-10 09:48:34 -05:00
2022-04-06 09:08:27 -04:00
```{code-block} python myapp.py
2023-10-09 13:39:24 -04:00
#!/usr/bin/env python
2021-12-10 09:48:34 -05:00
from flask import Flask
app = Flask(__name__)
@app .route("/")
def hello():
2022-12-09 22:33:06 -05:00
return {
"message": "Hello, Nix!"
}
2021-12-10 09:48:34 -05:00
def run():
2022-12-09 22:33:06 -05:00
app.run(host="0.0.0.0", port=5000)
2021-12-10 09:48:34 -05:00
if __name__ == "__main__":
run()
```
2024-01-28 20:44:42 -05:00
This is a simple Flask application which serves a JSON document with the message `"Hello, Nix!"` .
2022-12-09 22:33:06 -05:00
2024-01-28 20:44:42 -05:00
Create a new file `shell.nix` to declare the development environment:
2022-12-09 22:33:06 -05:00
```{code-block} nix shell.nix
2024-02-26 20:48:44 -05:00
{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11") {} }:
2021-12-10 09:48:34 -05:00
2024-02-06 15:16:17 -05:00
pkgs.mkShellNoCC {
2024-01-28 20:44:42 -05:00
packages = with pkgs; [
(python3.withPackages (ps: [ ps.flask ]))
curl
jq
2022-12-09 22:33:06 -05:00
];
}
2021-12-10 09:48:34 -05:00
```
2024-01-28 20:44:42 -05:00
This describes a shell environment with an instance of `python3` that includes the `flask` package using [`python3.withPackages` ](https://nixos.org/manual/nixpkgs/stable/#python.withpackages-function ).
2023-10-09 13:39:24 -04:00
It also contains [`curl`], a utility to perform web
requests, and [`jq`], a tool to parse and format JSON documents.
2022-12-09 22:33:06 -05:00
2024-01-28 20:44:42 -05:00
[`curl`]: https://search.nixos.org/packages?show=curl
[`jq`]: https://search.nixos.org/packages?show=jq
2022-12-09 22:33:06 -05:00
2023-10-09 13:39:24 -04:00
Both of them are not Python packages.
2024-01-28 20:44:42 -05:00
If you went with Python's [virtualenv ](https://virtualenv.pypa.io/en/latest/ ), it would not be possible to add these utilities to the development environment without additional manual steps.
2023-10-09 13:39:24 -04:00
2024-01-28 20:44:42 -05:00
Run `nix-shell` to enter the environment you just declared:
2021-12-10 09:48:34 -05:00
2022-11-08 11:37:41 -05:00
```shell-session
2022-12-09 22:33:06 -05:00
$ nix-shell
these 2 derivations will be built:
/nix/store/5yvz7zf8yzck6r9z4f1br9sh71vqkimk-builder.pl.drv
/nix/store/aihgjkf856dbpjjqalgrdmxyyd8a5j2m-python3-3.9.13-env.drv
these 93 paths will be fetched (109.50 MiB download, 468.52 MiB unpacked):
/nix/store/0xxjx37fcy2nl3yz6igmv4mag2a7giq6-glibc-2.33-123
/nix/store/138azk9hs5a2yp3zzx6iy1vdwi9q26wv-hook
...
2024-01-28 20:44:42 -05:00
[nix-shell:~]$
2021-12-10 09:48:34 -05:00
```
2023-10-09 13:39:24 -04:00
Start the web application within this shell environment:
2021-12-10 09:48:34 -05:00
2022-11-08 11:37:41 -05:00
```shell-session
2024-01-28 20:44:42 -05:00
[nix-shell:~]$ python ./myapp.py
2022-12-09 22:33:06 -05:00
* Serving Flask app 'myapp'
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on all addresses (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://192.168.1.100:5000
Press CTRL+C to quit
2021-12-10 09:48:34 -05:00
```
2023-10-09 13:39:24 -04:00
You now have a running Python web application.
Try it out!
2024-01-28 20:44:42 -05:00
2023-10-09 13:39:24 -04:00
Open a new terminal to start another session of the shell environment and follow the commands below:
2022-04-06 09:08:27 -04:00
2022-12-09 22:33:06 -05:00
```shell-session
$ nix-shell
2022-04-06 09:08:27 -04:00
2024-01-28 20:44:42 -05:00
[nix-shell:~]$ curl 127.0.0.1:5000
2022-12-09 22:33:06 -05:00
{"message":"Hello, Nix!"}
2022-04-06 09:08:27 -04:00
2024-01-28 20:44:42 -05:00
[nix-shell:~]$ curl 127.0.0.1:5000 | jq '.message'
2022-12-09 22:33:06 -05:00
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 26 100 26 0 0 13785 0 --:--:-- --:--:-- --:--:-- 26000
"Hello, Nix!"
2022-04-06 09:08:27 -04:00
```
2022-12-09 22:33:06 -05:00
2024-01-28 20:44:42 -05:00
As demonstrated, you can use both `curl` and `jq` to test the running web application without any manual installation.
You can commit the files we created to version control and share them with other people.
Others can now use the same shell environment as long as they have [Nix installed ](install-nix ).
## Next steps
2022-12-09 22:33:06 -05:00
2024-03-07 22:15:19 -05:00
- [](packaging-tutorial)
2024-02-24 08:18:27 -05:00
- [](file-sets-tutorial)
2024-01-28 20:44:42 -05:00
- [](automatic-direnv)
- [](./dependency-management.md)