Last active
November 18, 2024 21:21
-
-
Save jaysin586/ba1d1bc1547c0bc0fac6bcab4e849a8d to your computer and use it in GitHub Desktop.
PyENV Update Script
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
# A function to update the python version in a pyenv virtualenv | |
# Usage: pyenv_update <version> | |
# Example: pyenv_update 3.9.7 | |
# Note: This will remove the virtualenv and recreate it. | |
pyenv_update() { | |
PYENV_FILE=.python-version | |
PYENV_FREEZE_FILE=.requirements-lock.txt | |
# Check if version argument is provided | |
if [ -z "$1" ]; then | |
echo "β Error: Please specify the version of python to update to." | |
echo "Usage: pyenv_update <version>" | |
echo "Example: pyenv_update 3.9.7" | |
return 1 | |
fi | |
# Check if .python-version exists | |
if [ ! -f "$PYENV_FILE" ]; then | |
echo "β Error: $PYENV_FILE does not exist in current directory." | |
return 1 | |
fi | |
echo "π Current directory: $(pwd)" | |
pyenv_version=$(head -n 1 $PYENV_FILE) | |
echo "π¦ Updating from Python $pyenv_version to $1" | |
# Install new Python version if needed | |
echo "π§ Installing Python $1 if not already installed..." | |
pyenv install -s "$1" | |
# Save current dependencies | |
echo "π Saving current dependencies..." | |
pip freeze > $PYENV_FREEZE_FILE | |
# Remove old virtualenv | |
echo "ποΈ Removing old virtualenv..." | |
pyenv uninstall -f $pyenv_version | |
# Create new virtualenv | |
echo "ποΈ Creating new virtualenv..." | |
pyenv virtualenv $1 $pyenv_version | |
# Upgrade pip and reinstall dependencies | |
echo "πΌ Upgrading pip..." | |
pip install --upgrade pip >/dev/null 2>&1 | |
echo "π₯ Reinstalling dependencies..." | |
pip install -r $PYENV_FREEZE_FILE >/dev/null 2>&1 | |
# Cleanup | |
rm $PYENV_FREEZE_FILE | |
echo "β Successfully updated to Python $1" | |
echo "π Current Python version: $(python --version)" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ur the best <3