Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Change .venv script to be more compatible with IDEs #2259

Merged
merged 6 commits into from
Sep 11, 2024

Conversation

Lagoja
Copy link
Contributor

@Lagoja Lagoja commented Sep 9, 2024

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?

@Lagoja Lagoja marked this pull request as ready for review September 10, 2024 15:58
@Lagoja
Copy link
Contributor Author

Lagoja commented Sep 10, 2024

Question: Do we care about preserving a user's pre-existing .venv if they created it with a different tool? Or should we just always squash it.

@loreto
Copy link
Contributor

loreto commented Sep 10, 2024

Question: Do we care about preserving a user's pre-existing .venv if they created it with a different tool? Or should we just always squash it.

Preserve it.

@@ -1,8 +1,92 @@
#!/bin/sh
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why get rid of the shebang?

#!/bin/sh
set -eu
STATE_FILE="$DEVBOX_PROJECT_ROOT/.devbox/venv_check_completed"
echo $STATE_FILE
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this left in from debugging?

fi

# Check Python version
if ! check_python_version; then
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we just checking the version to see if venv is available? If so, it might be easier to just try importing it.

Suggested change
if ! check_python_version; then
if ! python -c "import venv" &>/dev/null; then

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that's a much nicer approach

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to tweak this because for some reason it was failing the automated tests (but not when run locally)

echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "Overwriting existing virtual environment..."
rm -rf "$VENV_DIR"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can use python3 -m venv --clear to overwrite.

}

# Function to check if Python is a symlink to a Devbox Python
is_devbox_python() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also the test -ef flag to check if two files are the same. So you could do:

if [ "$1/bin/python" -ef "$DEVBOX_PACKAGES_DIR/bin/python" ]; then

@Lagoja Lagoja requested a review from gcurtis September 10, 2024 21:44

create_venv() {
python -m venv "$VENV_DIR" --clear
echo "*\n.*" >> "$VENV_DIR/.gitignore"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

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"
touch "$STATE_FILE"
exit 1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this work after the user has updated their python version?

i.e. by creating the state-file, will it skip running again to create the venv after upgrading python

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should, because the python in the venv is a symlink to the python in our Devbox profile, which is a symlink to the nix store. I'll test a few times to make sure

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed, once the .venv is created, updating python automatically updates the python linked in the .venv folder

@Lagoja Lagoja merged commit 6df893a into main Sep 11, 2024
24 checks passed
@Lagoja Lagoja deleted the jl/tweak-venv-script branch September 11, 2024 17:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

5 participants