Last active
April 20, 2024 08:15
-
-
Save josfaber/99f16c31c08517ed8bc2a9d35f4b60bc to your computer and use it in GitHub Desktop.
Start Python virtual environment and install packages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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