Skip to content

Commit

Permalink
[Python] Venv tweaks based on testing in Sandboxes (#2264)
Browse files Browse the repository at this point in the history
## 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 <>
  • Loading branch information
Lagoja committed Sep 12, 2024
1 parent 6df893a commit d1ab641
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
3 changes: 2 additions & 1 deletion plugins/pip.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "0.0.2",
"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",
"env": {
"VENV_DIR": "{{ .Virtenv }}/.venv"
"VENV_DIR": "{{ .Virtenv }}/.venv",
"UV_PYTHON": "{{ .DevboxProjectRoot }}/.devbox/nix/profile/default/bin/python"
},
"create_files": {
"{{ .Virtenv }}/bin/venvShellHook.sh": "pip/venvShellHook.sh"
Expand Down
11 changes: 5 additions & 6 deletions plugins/pip/venvShellHook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ create_venv() {
echo "*\n.*" >> "$VENV_DIR/.gitignore"
}

# Check if we've already run this script
if [ -f "$STATE_FILE" ]; then
# "We've already run this script. Exiting..."
exit 0
fi

# Check that Python version supports venv
if ! python -c 'import venv' 1> /dev/null 2> /dev/null; then
echo "\033[1;33mWARNING: Python version must be > 3.3 to create a virtual environment.\033[0m"
Expand All @@ -31,6 +25,11 @@ fi
# Check if the directory exists
if [ -d "$VENV_DIR" ]; then
if is_valid_venv "$VENV_DIR"; then
# Check if we've already run this script
if [ -f "$STATE_FILE" ]; then
# "We've already run this script. Exiting..."
exit 0
fi
if ! is_devbox_venv "$VENV_DIR"; then
echo "\033[1;33mWARNING: Virtual environment at $VENV_DIR doesn't use Devbox Python.\033[0m"
read -p "Do you want to overwrite it? (y/n) " -n 1 -r
Expand Down

0 comments on commit d1ab641

Please sign in to comment.