Skip to content

Instantly share code, notes, and snippets.

@josfaber
Last active April 20, 2024 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josfaber/99f16c31c08517ed8bc2a9d35f4b60bc to your computer and use it in GitHub Desktop.
Save josfaber/99f16c31c08517ed8bc2a9d35f4b60bc to your computer and use it in GitHub Desktop.
Start Python virtual environment and install packages
#!/bin/sh
# USAGE:
# source start
venv_folder=".venv"
packages=(
"pygame==2.5.2"
"numpy==1.26.0"
# add more packages here
)
python_execs=(
$(command -v python)
$(command -v python3)
$(command -v python3.11)
$(command -v python3.10)
)
# Check if a Python virtual environment is active and deactivate it
if [[ $VIRTUAL_ENV != "" ]]; then
deactivate
fi
# Find Python executable
for exec in "${python_execs[@]}"; do
if [[ -x "$exec" ]]; then
python_exec="$exec"
break
fi
done
if [[ -z "$python_exec" ]]; then
echo "Python executable not found"
exit 1
fi
# Create a virtual environment if it does not exist
if [[ ! -d $venv_folder ]]; then
install_requirements=true
$python_exec -m venv $venv_folder
fi
# Activate the virtual environment
echo "Activating virtual environment"
source $venv_folder/bin/activate
# Install requirements if the virtual environment was just created
if [[ $install_requirements == true ]]; then
echo "Installing requirements"
for package in "${packages[@]}"; do
pip install "$package"
done
unset install_requirements
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment