Activate your Python Virtual Environment with ease using Bash scripting!

in Learning Python28 days ago (edited)

Hey!

After having dabbled with Python scripts and constantly having to create and activate the virtual environment again each time creating a new, or accessing an old project, I decided to whip up a quick Bash script that does it all for me.

It checks whether there is a Python Virtual Environment already set up and activated, and if not, it will create and/or activate it for you with one quick command.

You can save this script in your ~/bin or ~/bin/scripts (remember to have your $PATH variable point to the directory though), and use a special alias to access it, so it will always create and/or activate the virtual environment in your current directory!

alias ac='. ~/bin/scripts/activate_venv.sh'

Edit, small update:

The reason for using this "dot alias" (. ~/bin/scripts/activate_venv.sh) is that it keeps the virtual environment active after the script finishes, unlike a typical alias which would end the activation as soon as the script completes.


Here's the script:

#!/bin/bash

# Function to show some animated dots
show_progress() {
  while true; do
    echo -n "."
    sleep 0.5
  done
}

# Check if we're already in a virtual environment
if [[ -z "$VIRTUAL_ENV" ]]; then
  # Check if a virtual environment exists in the current directory
  if [[ ! -d "venv" ]]; then
    echo -n "Creating a virtual environment in the current directory"

    # Start the animated dots in the background
    show_progress &
    # Save the background process ID
    PROGRESS_PID=$!

    # Create the virtual environment
    python3 -m venv venv

    # Stop the animated dots
    kill "$PROGRESS_PID" 2>/dev/null
    wait "$PROGRESS_PID" 2>/dev/null
    echo # Newline
  fi

  # Animated venv activation sequence
  echo -n "Activating the virtual environment"
  show_progress &
  PROGRESS_PID=$!
  #Activate the virtual environment
  source venv/bin/activate
  sleep 1.5
  kill "$PROGRESS_PID" 2>/dev/null
  wait "$PROGRESS_PID" 2>/dev/null
  echo # Newline

else
  echo "Already in a virtual environment."
fi

To add a bit of polish (and fun), I added a dot animation to the script. While the virtual environment is being created, you’ll see animated dots indicating progress, giving a visual cue that something’s happening. I also added an extra sleep 1.5 during the activation sequence to extend the animation a bit, as the activation process itself is near-instantaneous. The brief pause lets the dots run through a complete cycle, making it look like the script is "gearing up" to activate the environment. It’s a small touch, but it adds that sense of action, even if it’s just for show!

I hope you like it...

Tell me if you use similar helper scripts with whatever you do with your computer!

Sort:  

Thanks for your contribution to the STEMsocial community. Feel free to join us on discord to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.