Skip to content

Commit d1ab641

Browse files
authored
[Python] Venv tweaks based on testing in Sandboxes (#2264)
## Summary A few small tweaks after testing in Sandboxes 1. Move the `STATE_CHECK` to after we verify that a VENV exists and that it's properly formatted. This ensures that we can fix scenarios where the venv was deleted or broken, without excessively prompting the user 2. Add a `UV_PYTHON` environment variable to ensure that users who install `uv` don't accidentally overwrite the Python version in their `devbox.json` ## How was it tested? 1. Open the `python-flask` Jetify Example in Sandboxes 2. Test `.venv` is setup correctly with `devbox shell` 3. Delete the `.venv` dir, verify that re-running `devbox shell` creates it 4. Install `uv` (`devbox add uv`) 5. Verify that `uv sync` and other commands don't override Devbox Python Co-authored-by: John Lago <>
1 parent 6df893a commit d1ab641

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

plugins/pip.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.0.2",
44
"description": "This plugin adds a script for automatically creating 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-
"VENV_DIR": "{{ .Virtenv }}/.venv"
6+
"VENV_DIR": "{{ .Virtenv }}/.venv",
7+
"UV_PYTHON": "{{ .DevboxProjectRoot }}/.devbox/nix/profile/default/bin/python"
78
},
89
"create_files": {
910
"{{ .Virtenv }}/bin/venvShellHook.sh": "pip/venvShellHook.sh"

plugins/pip/venvShellHook.sh

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@ create_venv() {
1515
echo "*\n.*" >> "$VENV_DIR/.gitignore"
1616
}
1717

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
22-
fi
23-
2418
# Check that Python version supports venv
2519
if ! python -c 'import venv' 1> /dev/null 2> /dev/null; then
2620
echo "\033[1;33mWARNING: Python version must be > 3.3 to create a virtual environment.\033[0m"
@@ -31,6 +25,11 @@ fi
3125
# Check if the directory exists
3226
if [ -d "$VENV_DIR" ]; then
3327
if is_valid_venv "$VENV_DIR"; then
28+
# Check if we've already run this script
29+
if [ -f "$STATE_FILE" ]; then
30+
# "We've already run this script. Exiting..."
31+
exit 0
32+
fi
3433
if ! is_devbox_venv "$VENV_DIR"; then
3534
echo "\033[1;33mWARNING: Virtual environment at $VENV_DIR doesn't use Devbox Python.\033[0m"
3635
read -p "Do you want to overwrite it? (y/n) " -n 1 -r

0 commit comments

Comments
 (0)