Skip to content

Instantly share code, notes, and snippets.

@dansimau
Created January 7, 2014 11:29
Show Gist options
  • Save dansimau/8298049 to your computer and use it in GitHub Desktop.
Save dansimau/8298049 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Get last argument as the base filename
file=${!#}
if [ "$file" == "--version" ]; then
python --version
exit $?
fi
venv_path=""
# Start at the dir of the file
basedir=$(dirname $file)
# Loop through each dir, going up one each time
while [ "$basedir" != "/" ]; do
# Check for venv dirs
for dir in .venv venv; do
if [ -d "$basedir/$dir" ]; then
# Break when we've found one
venv_path="$basedir/$dir"
break 2
fi
done
# Go up one level
basedir=$(dirname $basedir)
done
# Detect site-package dirs in venv/lib and add them to path
for dir in $venv_path/lib/*/site-packages; do
PYTHONPATH=$PYTHONPATH:$dir
done
# Run python with the venv lib dirs in the path
export PYTHONPATH
exec python "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment