Skip to content

Commit fd04fe0

Browse files
authored
[Plugin] Create a virtual environment with the python plugin (#1645)
## Summary The Nix Python packages come bundled with `pip`, so we can go ahead and create a virtual environment whenever a user installs a `python` package, even if they do not also install `pip`. This plugin reuses the `venvShellHook` and `VENV_DIR` environment variable from the `pip` plugin to create a virtual environment when the user starts their shell (assuming one has not been created). The user can activate the virtual environment by running `source $VENV_DIR/bin/activate` A few notes * This should not cause issues with virtual environments that have been created with `pythonPackages.pip`, meaning behavior should remain the same for existing python projects * Devbox will use `VENV_DIR` variable and virtual environment for the package the appears last in the `packages` list. * Going forward, we should probably recommend developers use the `pip` bundled with the python package. ## How was it tested? Using the modified python/pip example in this PR
1 parent 5a6f585 commit fd04fe0

File tree

5 files changed

+42
-40
lines changed

5 files changed

+42
-40
lines changed

docs/app/docs/devbox_examples/languages/python.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,27 @@ This will install Python 3.10 in your shell. You can find other versions of Pyth
2727

2828
[pip](https://pip.pypa.io/en/stable/) is the standard package manager for Python. Since it installs python packages globally, we strongly recommend using a virtual environment.
2929

30-
You can install `pip` by running `devbox add python3xxPackages.pip`, where `3xx` is the version of Python you want to install. This will also install the pip plugin for Devbox, which automatically creates a virtual environment for installing your packages locally
30+
The `python` package automatically comes bundled with `pip`, and the `python` plugin for Devbox will automatically create a virtual environment for installing your packages locally
3131

32-
Your virtual environment is created in the `.devbox/virtenv/pip` directory by default, and can be activated by running `source $VENV_DIR/bin/activate` in your devbox shell. You can activate the virtual environment automatically using the init_hook of your `devbox.json`:
32+
Your virtual environment is created in the `.devbox/virtenv/python` directory by default, and can be activated by running `source $VENV_DIR/bin/activate` in your devbox shell. You can activate the virtual environment automatically using the init_hook of your `devbox.json`:
3333

3434
```json
3535
{
3636
"packages": [
37-
"python310",
37+
38+
],
39+
"shell": {
40+
"init_hook": ". $VENV_DIR/bin/activate"
41+
}
42+
}
43+
```
44+
45+
If you need to install a specific version of Pip, you can run `devbox add python3xxPackages.pip`, where `3xx` is the major + minor version (e.g., python310 = [email protected]) of Python you want to install:
46+
47+
```json
48+
{
49+
"packages": [
50+
3851
"python310Packages.pip"
3952
],
4053
"shell": {
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
{
2-
"packages": [
3-
4-
2+
"packages": [
3+
4+
],
5+
"shell": {
6+
"init_hook": [
7+
". $VENV_DIR/bin/activate",
8+
"pip install -r requirements.txt"
59
],
6-
"shell": {
7-
"init_hook": [
8-
". $VENV_DIR/bin/activate",
9-
"pip install -r requirements.txt"
10-
],
11-
"scripts": {
12-
"run_test": "python main.py"
13-
}
10+
"scripts": {
11+
"run_test": "python main.py"
1412
}
13+
}
1514
}

examples/development/python/pip/devbox.lock

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,9 @@
11
{
22
"lockfile_version": "1",
33
"packages": {
4-
5-
"last_modified": "2023-05-01T16:53:22Z",
6-
"plugin_version": "0.0.1",
7-
"resolved": "github:NixOS/nixpkgs/8670e496ffd093b60e74e7fa53526aa5920d09eb#python310Packages.pip",
8-
"version": "23.0.1",
9-
"systems": {
10-
"aarch64-darwin": {
11-
"store_path": "/nix/store/idd0pswjhxrqp7mdgrhw6rmpmlkrya8r-python3.10-pip-23.0.1"
12-
},
13-
"aarch64-linux": {
14-
"store_path": "/nix/store/ma7lp6a1gbss1aa2mzp2jkxgsvp9m0pg-python3.10-pip-23.0.1"
15-
},
16-
"x86_64-darwin": {
17-
"store_path": "/nix/store/77sd3ym5f7p5zn7bmfz36wrnklfq4ic9-python3.10-pip-23.0.1"
18-
},
19-
"x86_64-linux": {
20-
"store_path": "/nix/store/gxb4vyiqip38lzhjsa2ijhb3wjf4sp4b-python3.10-pip-23.0.1"
21-
}
22-
}
23-
},
244
255
"last_modified": "2023-02-24T18:08:35Z",
26-
"plugin_version": "0.0.1",
6+
"plugin_version": "0.0.2",
277
"resolved": "github:NixOS/nixpkgs/806075be2bdde71895359ed18cb530c4d323e6f6#python3",
288
"source": "devbox-search",
299
"version": "3.10.9",

plugins/pip/venvShellHook.sh

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
#!/bin/sh
22

3-
if [ -d "$VENV_DIR" ]; then
4-
echo "Skipping venv creation, '${VENV_DIR}' already exists"
5-
else
3+
if ! [ -d "$VENV_DIR" ]; then
64
echo "Creating new venv environment in path: '${VENV_DIR}'"
75
python3 -m venv "$VENV_DIR"
86
fi
7+
98
echo "You can activate the virtual environment by running 'source \$VENV_DIR/bin/activate'"

plugins/python.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
{
22
"name": "python",
3-
"version": "0.0.1",
4-
"readme": "python on devbox works best when used with a virtual environment (vent, virtualenv, etc). For example with python3:\n\n> python -m venv .venv\n> source .venv/bin/activate\n\nPackage managers like poetry (https://python-poetry.org/) automatically create virtual environments for you."
3+
"version": "0.0.2",
4+
"readme": "Python in Devbox works best when used with a virtual environment (vent, virtualenv, etc.). Devbox will automatically create a virtual environment using `venv` for python3 projects, so you can install packages with pip as normal.\nTo activate the environment, run `source $VENV_DIR/bin/activate` or add it to the init_hook of your devbox.json\nTo change where your virtual environment is created, modify the $VENV_DIR environment variable in your init_hook",
5+
"env": {
6+
"VENV_DIR": "{{ .Virtenv }}/.venv"
7+
},
8+
"create_files": {
9+
"{{ .Virtenv }}/bin/venvShellHook.sh": "pip/venvShellHook.sh"
10+
},
11+
"shell": {
12+
"init_hook": [
13+
"{{ .Virtenv }}/bin/venvShellHook.sh"
14+
]
15+
}
516
}

0 commit comments

Comments
 (0)