Skip to content

Instantly share code, notes, and snippets.

@munabedan
Created January 20, 2024 10:34
Show Gist options
  • Save munabedan/6a5e8c104228943a461095a9e103a5af to your computer and use it in GitHub Desktop.
Save munabedan/6a5e8c104228943a461095a9e103a5af to your computer and use it in GitHub Desktop.
Making python virtual environments less of an annoyance to use
venv() {
# Check if already activated
if [[ "$VIRTUAL_ENV" != "" ]]; then
echo -e "\n\e[1;33mDeactivating current virtual environment...\e[0m"
deactivate
return
fi
# Check if the venv directory exists
if [ -d "venv" ]; then
echo -e "\n\e[1;33mActivating virtual environment...\e[0m"
source venv/bin/activate
else
echo -e "\n\e[1;33mCreating and activating virtual environment...\e[0m"
python3 -m venv venv
source venv/bin/activate
fi
}
@Itsfoss0
Copy link

For development purposes on my local box, I have all the virtual envs in a centralized location /home/$USER/virtualsevs/

image

Your current resource config assumes that I have (or will need) a virtual environment in the CWD. Is there a way to ask the user for the path they want to configure their virtual environment at?

@munabedan
Copy link
Author

I guess I can add that in but currently it just creates a venv in the default DIR which happens to be CWD by running

python3 -m venv venv

You can always change the above to be

python -m venv /path/to/new/virtual/environment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment