Skip to content

Commit 6df893a

Browse files
authored
[Python] Change .venv script to be more compatible with IDEs (#2259)
## Summary Moves `.venv` to the Devbox Project Root, and adds some checks to ensure we warn the user if we are going to squash a user created venv. Moving `.venv` to the project root makes it more likely that Python Extensions and IDEs will pick up the devbox managed python, instead of a system python Fixes DEV-105 ## How was it tested? --------- Co-authored-by: John Lago <>
1 parent aaf5670 commit 6df893a

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed

plugins/pip/venvShellHook.sh

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,54 @@
11
#!/bin/sh
2+
set -eu
3+
STATE_FILE="$DEVBOX_PROJECT_ROOT/.devbox/venv_check_completed"
24

3-
if ! [ -d "$VENV_DIR" ]; then
4-
echo "Creating new venv environment in path: '${VENV_DIR}'"
5-
python3 -m venv "$VENV_DIR"
6-
echo "You can activate the virtual environment by running '. \$VENV_DIR/bin/activate' (for fish shell, replace '.' with 'source')" >&2
5+
is_valid_venv() {
6+
[ -f "$1/bin/activate" ] && [ -f "$1/bin/python" ]
7+
}
8+
9+
is_devbox_venv() {
10+
[ "$1/bin/python" -ef "$DEVBOX_PACKAGES_DIR/bin/python" ]
11+
}
12+
13+
create_venv() {
14+
python -m venv "$VENV_DIR" --clear
15+
echo "*\n.*" >> "$VENV_DIR/.gitignore"
16+
}
17+
18+
# Check if we've already run this script
19+
if [ -f "$STATE_FILE" ]; then
20+
# "We've already run this script. Exiting..."
21+
exit 0
722
fi
823

24+
# Check that Python version supports venv
25+
if ! python -c 'import venv' 1> /dev/null 2> /dev/null; then
26+
echo "\033[1;33mWARNING: Python version must be > 3.3 to create a virtual environment.\033[0m"
27+
touch "$STATE_FILE"
28+
exit 1
29+
fi
30+
31+
# Check if the directory exists
32+
if [ -d "$VENV_DIR" ]; then
33+
if is_valid_venv "$VENV_DIR"; then
34+
if ! is_devbox_venv "$VENV_DIR"; then
35+
echo "\033[1;33mWARNING: Virtual environment at $VENV_DIR doesn't use Devbox Python.\033[0m"
36+
read -p "Do you want to overwrite it? (y/n) " -n 1 -r
37+
echo
38+
if [[ $REPLY =~ ^[Yy]$ ]]; then
39+
echo "Overwriting existing virtual environment..."
40+
create_venv
41+
else
42+
echo "Using your existing virtual environment. We recommend changing \$VENV_DIR to a different location"
43+
touch "$STATE_FILE"
44+
exit 1
45+
fi
46+
fi
47+
else
48+
echo "Directory exists but is not a valid virtual environment. Creating a new one..."
49+
create_venv
50+
fi
51+
else
52+
echo "Virtual environment directory doesn't exist. Creating new one..."
53+
create_venv
54+
fi

plugins/python.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
{
22
"name": "python",
3-
"version": "0.0.3",
4-
"description": "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 `. $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",
3+
"version": "0.0.4",
4+
"description": "Python in Devbox works best when used with a virtual environment (venv, 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 `. $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",
55
"env": {
6-
/*
7-
This is a block comment
8-
*/
9-
"VENV_DIR": "{{ .Virtenv }}/.venv"
6+
"VENV_DIR": "{{ .DevboxProjectDir }}/.venv"
107
},
118
"create_files": {
12-
"{{ .Virtenv }}/bin/venvShellHook.sh": "pip/venvShellHook.sh"
9+
"{{ .Virtenv }}/bin/venvShellHook.sh": "pip/venvShellHook.sh"
1310
},
14-
// this is a line comment above shell
1511
"shell": {
16-
"init_hook": [
17-
"{{ .Virtenv }}/bin/venvShellHook.sh"
18-
]
12+
"init_hook": ["{{ .Virtenv }}/bin/venvShellHook.sh"]
1913
}
2014
}

0 commit comments

Comments
 (0)