python3.11 support

The script will now detect and utilize Python 3.11 alongside Python 3.10.

Here are the key changes:
- I added a function to detect the available Python command, prioritizing python3.11, then python3.10, then python3.
- I modified virtual environment creation to use the detected Python version.
- I updated macOS Homebrew installation logic to check for python@3.11 and python-tk@3.11 first.
- I made Python-version-specific paths (e.g., for site-packages and symlinks, especially in Runpod environments) dynamic to adapt to the selected Python version.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
pull/3306/head
bmaltais 2025-06-20 07:30:38 -04:00 committed by GitHub
parent dcef66a001
commit b48c86cbff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 66 additions and 38 deletions

104
setup.sh
View File

@ -1,6 +1,19 @@
#!/usr/bin/env bash #!/usr/bin/env bash
cd "$(dirname "$0")" cd "$(dirname "$0")"
# Function to get the python command
get_python_command() {
if command -v python3.11 &>/dev/null; then
echo "python3.11"
elif command -v python3.10 &>/dev/null; then
echo "python3.10"
elif command -v python3 &>/dev/null; then
echo "python3"
else
echo "python" # Fallback, though this might not have venv
fi
}
# Function to display help information # Function to display help information
display_help() { display_help() {
cat <<EOF cat <<EOF
@ -188,27 +201,29 @@ create_symlinks() {
# Function to install Python dependencies # Function to install Python dependencies
install_python_dependencies() { install_python_dependencies() {
local TEMP_REQUIREMENTS_FILE local TEMP_REQUIREMENTS_FILE
local PYTHON_CMD
PYTHON_CMD=$(get_python_command)
if [ "$PYTHON_CMD" == "python" ]; then # Check if get_python_command returned the fallback
echo "Could not find python3.11, python3.10, or python3."
echo "Please install a compatible Python version."
return 1
fi
# Switch to local virtual env # Switch to local virtual env
echo "Switching to virtual Python environment." echo "Switching to virtual Python environment using $PYTHON_CMD."
if ! inDocker; then if ! inDocker; then
# Check if conda environment is already activated # Check if conda environment is already activated
if [ -n "$CONDA_PREFIX" ]; then if [ -n "$CONDA_PREFIX" ]; then
echo "Detected active conda environment: $CONDA_DEFAULT_ENV" echo "Detected active conda environment: $CONDA_DEFAULT_ENV"
echo "Using existing conda environment at: $CONDA_PREFIX" echo "Using existing conda environment at: $CONDA_PREFIX"
# No need to create or activate a venv, conda env is already active # No need to create or activate a venv, conda env is already active
elif command -v python3.10 >/dev/null; then
python3.10 -m venv "$DIR/venv"
# Activate the virtual environment
source "$DIR/venv/bin/activate"
elif command -v python3 >/dev/null; then
python3 -m venv "$DIR/venv"
# Activate the virtual environment
source "$DIR/venv/bin/activate"
else else
echo "Valid python3 or python3.10 binary not found." "$PYTHON_CMD" -m venv "$DIR/venv"
echo "Cannot proceed with the python steps." # Activate the virtual environment
return 1 # shellcheck source=/dev/null
source "$DIR/venv/bin/activate"
fi fi
fi fi
@ -547,35 +562,37 @@ if [[ "$OSTYPE" == "lin"* ]]; then
if [ "$RUNPOD" = true ]; then if [ "$RUNPOD" = true ]; then
if inDocker; then if inDocker; then
# We get the site-packages from python itself, then cut the string, so no other code changes required. # We get the site-packages from python itself, then cut the string, so no other code changes required.
VENV_DIR=$(python -c "import site; print(site.getsitepackages()[0])") PYTHON_CMD_FALLBACK=$(get_python_command) # Use a fallback if PYTHON_CMD is not set (e.g. not called from install_python_dependencies)
VENV_DIR="${VENV_DIR%/lib/python3.10/site-packages}" VENV_PYTHON_VERSION=$($PYTHON_CMD_FALLBACK -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
VENV_DIR=$($PYTHON_CMD_FALLBACK -c "import site; print(site.getsitepackages()[0])")
VENV_DIR="${VENV_DIR%/lib/python${VENV_PYTHON_VERSION}/site-packages}"
fi fi
# Symlink paths # Symlink paths
libnvinfer_plugin_symlink="$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer_plugin.so.7" libnvinfer_plugin_symlink="$VENV_DIR/lib/python${VENV_PYTHON_VERSION}/site-packages/tensorrt/libnvinfer_plugin.so.7"
libnvinfer_symlink="$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer.so.7" libnvinfer_symlink="$VENV_DIR/lib/python${VENV_PYTHON_VERSION}/site-packages/tensorrt/libnvinfer.so.7"
libcudart_symlink="$VENV_DIR/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/libcudart.so.11.0" libcudart_symlink="$VENV_DIR/lib/python${VENV_PYTHON_VERSION}/site-packages/nvidia/cuda_runtime/lib/libcudart.so.11.0"
#Target file paths #Target file paths
libnvinfer_plugin_target="$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer_plugin.so.8" libnvinfer_plugin_target="$VENV_DIR/lib/python${VENV_PYTHON_VERSION}/site-packages/tensorrt/libnvinfer_plugin.so.8"
libnvinfer_target="$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer.so.8" libnvinfer_target="$VENV_DIR/lib/python${VENV_PYTHON_VERSION}/site-packages/tensorrt/libnvinfer.so.8"
libcudart_target="$VENV_DIR/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/libcudart.so.12" libcudart_target="$VENV_DIR/lib/python${VENV_PYTHON_VERSION}/site-packages/nvidia/cuda_runtime/lib/libcudart.so.12"
# echo "Checking symlinks now." # echo "Checking symlinks now."
# create_symlinks "$libnvinfer_plugin_symlink" "$libnvinfer_plugin_target" # create_symlinks "$libnvinfer_plugin_symlink" "$libnvinfer_plugin_target"
# create_symlinks "$libnvinfer_symlink" "$libnvinfer_target" # create_symlinks "$libnvinfer_symlink" "$libnvinfer_target"
# create_symlinks "$libcudart_symlink" "$libcudart_target" # create_symlinks "$libcudart_symlink" "$libcudart_target"
# if [ -d "${VENV_DIR}/lib/python3.10/site-packages/tensorrt/" ]; then # if [ -d "${VENV_DIR}/lib/python${VENV_PYTHON_VERSION}/site-packages/tensorrt/" ]; then
# export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${VENV_DIR}/lib/python3.10/site-packages/tensorrt/" # export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${VENV_DIR}/lib/python${VENV_PYTHON_VERSION}/site-packages/tensorrt/"
# else # else
# echo "${VENV_DIR}/lib/python3.10/site-packages/tensorrt/ not found; not linking library." # echo "${VENV_DIR}/lib/python${VENV_PYTHON_VERSION}/site-packages/tensorrt/ not found; not linking library."
# fi # fi
# if [ -d "${VENV_DIR}/lib/python3.10/site-packages/tensorrt/" ]; then # if [ -d "${VENV_DIR}/lib/python${VENV_PYTHON_VERSION}/site-packages/tensorrt/" ]; then
# export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${VENV_DIR}/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/" # export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:${VENV_DIR}/lib/python${VENV_PYTHON_VERSION}/site-packages/nvidia/cuda_runtime/lib/"
# else # else
# echo "${VENV_DIR}/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/ not found; not linking library." # echo "${VENV_DIR}/lib/python${VENV_PYTHON_VERSION}/site-packages/nvidia/cuda_runtime/lib/ not found; not linking library."
# fi # fi
configure_accelerate configure_accelerate
@ -620,25 +637,36 @@ elif [[ "$OSTYPE" == "darwin"* ]]; then
check_storage_space check_storage_space
# Install base python packages # Install base python packages
echo "Installing Python 3.10 if not found." echo "Checking for Python 3.11 or 3.10."
if ! brew ls --versions python@3.10 >/dev/null; then if brew ls --versions python@3.11 >/dev/null; then
echo "Installing Python 3.10." echo "Python 3.11 found!"
brew install python@3.10 >&3 PYTHON_BREW_VERSION="python@3.11"
else elif brew ls --versions python@3.10 >/dev/null; then
echo "Python 3.10 found!" echo "Python 3.10 found!"
fi PYTHON_BREW_VERSION="python@3.10"
echo "Installing Python-TK 3.10 if not found."
if ! brew ls --versions python-tk@3.10 >/dev/null; then
echo "Installing Python TK 3.10."
brew install python-tk@3.10 >&3
else else
echo "Python Tkinter 3.10 found!" echo "Neither Python 3.11 nor 3.10 found via Homebrew. Installing Python 3.11."
brew install python@3.11 >&3
PYTHON_BREW_VERSION="python@3.11"
fi
echo "Installing $PYTHON_BREW_VERSION if not linked or found."
brew install "$PYTHON_BREW_VERSION" >&3
echo "Checking for Python TK for $PYTHON_BREW_VERSION."
PYTHON_TK_BREW_VERSION="python-tk@${PYTHON_BREW_VERSION#*@}" # Extracts e.g., 3.11 from python@3.11
if ! brew ls --versions "$PYTHON_TK_BREW_VERSION" >/dev/null; then
echo "Installing $PYTHON_TK_BREW_VERSION."
brew install "$PYTHON_TK_BREW_VERSION" >&3
else
echo "$PYTHON_TK_BREW_VERSION found!"
fi fi
update_kohya_ss update_kohya_ss
if ! install_python_dependencies; then if ! install_python_dependencies; then
echo "You may need to install Python. The command for this is brew install python@3.10." echo "You may need to install Python. The command for this is brew install $PYTHON_BREW_VERSION."
fi fi
configure_accelerate configure_accelerate