Created
July 17, 2025 14:34
-
-
Save Johnpc123/b4720b5c68ba813e62501be66c4ee47c to your computer and use it in GitHub Desktop.
Tired of hunting for your Python scripts and wrestling with virtual environments? This tool is your new best friend! It lets you run any Python utility with a simple command-line alias. Just point it to your script, and it automatically handles the boring stuff: creating a fresh virtual environment, upgrading pip to the latest version, and insta…
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/bash | |
| # alias_python.sh | |
| # | |
| # Purpose: | |
| # This script is designed to save effort when setting up and running Python utilities. It centralizes the process by allowing you to create simple aliases in your .bashrc, so you don't have to remember where each script is located. It also automates the creation of a Python virtual environment, upgrades pip, and installs any requirements listed in a requirements.txt file alongside your script. This makes it easy to keep your Python utilities isolated, up-to-date, and easy to run from anywhere. | |
| # | |
| # Usage: create an alias for this script, add the following line to your ~/.bashrc: | |
| # alias runmyscript='~/scripts/alias_python.sh <venv_path> <script_path>' | |
| # Replace <venv_path> and <script_path> as needed, or call the script directly with your parameters. | |
| # | |
| # | |
| # cli usage (): | |
| # alias_python.sh <venv_path> <script_path> [script_args ...] | |
| # | |
| # This script will: | |
| # 1. Check if the Python virtual environment exists at <venv_path>. | |
| # 2. If not, prompt to create it. If agreed, creates the venv, upgrades pip, and installs requirements.txt if present. | |
| # 3. Activate the venv and run <script_path> with any additional arguments passed to the script. | |
| # | |
| # Example: | |
| # alias myjob='~/scripts/alias_python.sh ~/myvenv ~/project/myscript.py' | |
| # myjob --foo bar | |
| set -e | |
| if [ $# -lt 2 ]; then | |
| echo "Usage: $0 <venv_path> <script_path> [script_args ...]" | |
| exit 1 | |
| fi | |
| VENV_PATH="$1" | |
| SCRIPT_PATH="$2" | |
| shift 2 | |
| if [ ! -d "$VENV_PATH" ]; then | |
| read -p "Python venv not found at $VENV_PATH. Create it now? [y/N]: " yn | |
| case $yn in | |
| [Yy]*) | |
| python3.12 -m venv "$VENV_PATH" | |
| source "$VENV_PATH/bin/activate" | |
| python -m pip install --upgrade pip | |
| REQ_FILE="$(dirname "$SCRIPT_PATH")/requirements.txt" | |
| if [ -f "$REQ_FILE" ]; then | |
| echo "Installing requirements from $REQ_FILE..." | |
| pip install -r "$REQ_FILE" | |
| fi | |
| deactivate | |
| ;; | |
| *) | |
| echo "Aborting." | |
| exit 1 | |
| ;; | |
| esac | |
| fi | |
| source "$VENV_PATH/bin/activate" | |
| python "$SCRIPT_PATH" "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment