Skip to content

Instantly share code, notes, and snippets.

@tevino
Last active March 24, 2024 09:41
Show Gist options
  • Save tevino/1a557a0c200d61d4e4fb to your computer and use it in GitHub Desktop.
Save tevino/1a557a0c200d61d4e4fb to your computer and use it in GitHub Desktop.
Fix python virtualenv after python update
#!/usr/bin/env bash
ENV_PATH="$(dirname "$(dirname "$(which pip)")")"
SYSTEM_VIRTUALENV="$(which -a virtualenv|tail -1)"
BAD_ENV_PATHS="/usr/local"
echo "Ensure the root of the broken virtualenv:"
echo " $ENV_PATH"
if [[ -z "$ENV_PATH" ]] || [[ "$ENV_PATH" = *"$BAD_ENV_PATHS"* ]]; then
echo "The root path above doesn't seems to be a valid one."
echo "Please make sure you ACTIVATED the broken virtualenv."
echo "‼️ Exiting for your safety... (thanks @laymonk for reporting this)"
exit 1
fi
read -p "‼️ Press Enter if you are not sure (y/N) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo "♻️ Removing old symbolic links......"
find "$ENV_PATH" -type l -delete -print
echo "💫 Creating new symbolic links......"
$SYSTEM_VIRTUALENV "$ENV_PATH"
echo "🎉 Done!"
fi
@EugeneDae
Copy link

Worked very well, thanks!

@de-la-viz
Copy link

Thanks!

@MazeFX
Copy link

MazeFX commented Jun 17, 2020

Worked like a charm! Had to deal with this issue multiple times now and this is the first working solution without creating a complete new env from scratch.

Thank you very much indeed!

@olitomas
Copy link

I found a better solution. Just recreate the virtualenv the same way you would normally create virtualenv. But use the folder path instead of the name you want for the virtualenv.

Example:

virtualenv -p python3.5 /path/to/your/existing/venv/

I had changed my system's Python version to 3.7.7 but the venv was python 3.5.2. It stopped working but this fixed it!

@mrhhyu
Copy link

mrhhyu commented Jan 25, 2021

Than you @olitomas. That was great

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment