caharkness.com

Home · Contact · Git


Python Setup

I work between both Windows and Linux and find myself writing the same shell script for every project I spin up. Here's now it's done:

#!/bin/bash

cd "$(dirname "$0")"

CREATE_VENV=0

if [[ ! -d venv ]]
then
    python3 -m venv venv || python -m venv venv || (
        echo "Could not create a Python virtual environment."
        exit 1
    )

    CREATE_VENV=1
fi

source ./venv/bin/activate
source ./venv/Scripts/activate

if [[ $CREATE_VENV -eq 1 ]]
then
    pip install dependency1 dependency2 etc
fi

python -u app.py $@

Keep in mind this is a shell script for Bash. To run on Windows, you must also have Bash on path. Once you do, this script works great and is portable between the big three: Windows, macOS, and Linux.