55# Behavior:
66# - Changes to the directory where this script resides
77# - If .deps_installed does not exist, installs requirements from requirements.txt
8+ # - Uses a virtual environment to avoid externally-managed-environment errors
89# - Only creates .deps_installed if installation succeeds
910# - Exits with non-zero code and message on failure
1011
@@ -13,41 +14,82 @@ set -euo pipefail
1314# Change to the directory where this script is located
1415cd -- " $( dirname -- " $0 " ) "
1516
16- # Helper to locate a working pip command
17- find_pip () {
18- if command -v python3 > /dev/null 2>&1 && python3 -m pip --version > /dev/null 2>&1 ; then
19- echo " python3 -m pip"
20- return 0
21- fi
22- if command -v python > /dev/null 2>&1 && python -m pip --version > /dev/null 2>&1 ; then
23- echo " python -m pip"
24- return 0
25- fi
26- if command -v pip3 > /dev/null 2>&1 ; then
27- echo " pip3"
17+ # Helper to find a working Python command
18+ find_python () {
19+ if command -v python3 > /dev/null 2>&1 ; then
20+ echo " python3"
2821 return 0
2922 fi
30- if command -v pip > /dev/null 2>&1 ; then
31- echo " pip "
23+ if command -v python > /dev/null 2>&1 ; then
24+ echo " python "
3225 return 0
3326 fi
3427 return 1
3528}
3629
30+ # Helper to create and use virtual environment
31+ setup_venv () {
32+ local python_cmd=" $1 "
33+ local venv_dir=" venv"
34+
35+ # Create virtual environment if it doesn't exist
36+ if [ ! -d " $venv_dir " ]; then
37+ echo " Creating virtual environment..."
38+ if ! " $python_cmd " -m venv " $venv_dir " ; then
39+ echo " Error: Failed to create virtual environment. Make sure python3-venv is installed." >&2
40+ echo " Try: sudo apt install python3-venv python3-full" >&2
41+ exit 1
42+ fi
43+ fi
44+
45+ # Return the path to the venv pip
46+ echo " $venv_dir /bin/pip"
47+ }
48+
3749if [ ! -f " .deps_installed" ]; then
3850 echo " Installing dependencies..."
3951
40- if ! PIP_CMD =$( find_pip ) ; then
41- echo " Error: pip not found. Please install Python and pip ." >&2
52+ if ! PYTHON_CMD =$( find_python ) ; then
53+ echo " Error: Python not found. Please install Python." >&2
4254 exit 1
4355 fi
4456
45- if ${PIP_CMD} install -q -r requirements.txt; then
57+ # Try to use system pip first, fall back to virtual environment on failure
58+ PIP_CMD=" "
59+
60+ # First, try to find system pip
61+ if command -v python3 > /dev/null 2>&1 && python3 -m pip --version > /dev/null 2>&1 ; then
62+ PIP_CMD=" python3 -m pip"
63+ elif command -v python > /dev/null 2>&1 && python -m pip --version > /dev/null 2>&1 ; then
64+ PIP_CMD=" python -m pip"
65+ elif command -v pip3 > /dev/null 2>&1 ; then
66+ PIP_CMD=" pip3"
67+ elif command -v pip > /dev/null 2>&1 ; then
68+ PIP_CMD=" pip"
69+ fi
70+
71+ # Try system pip installation first
72+ if [ -n " $PIP_CMD " ] && ${PIP_CMD} install -q -r requirements.txt 2> /dev/null; then
4673 # Create marker file to indicate dependencies are installed
4774 : > .deps_installed
48- echo " Dependencies installed."
75+ echo " Dependencies installed using system pip ."
4976 else
50- echo " Error: Failed to install dependencies." >&2
51- exit 1
77+ # System pip failed (likely externally-managed-environment), use virtual environment
78+ echo " System pip failed (likely externally managed environment). Using virtual environment..."
79+
80+ if ! VENV_PIP=$( setup_venv " $PYTHON_CMD " ) ; then
81+ echo " Error: Failed to setup virtual environment." >&2
82+ exit 1
83+ fi
84+
85+ if " $VENV_PIP " install -q -r requirements.txt; then
86+ # Create marker file to indicate dependencies are installed
87+ : > .deps_installed
88+ echo " Dependencies installed in virtual environment."
89+ echo " Note: Virtual environment created in ./venv directory"
90+ else
91+ echo " Error: Failed to install dependencies in virtual environment." >&2
92+ exit 1
93+ fi
5294 fi
5395fi
0 commit comments