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
@tevino
Copy link
Author

tevino commented Jun 11, 2015

The error message you may encounter is something like:

dyld: Library not loaded: @executable_path/../.Python
  Referenced from: /your/home/.virtualenvs/virtualenv_name/bin/python
  Reason: image not found
[1]    95221 trace trap  python

CAUTION: USE AT YOUR OWN RISK.

Just activate your broken virtualenv and execute this script, you can run it immediately with either curl or wget:

  • via curl
sh <(curl -sL https://gist.github.com/tevino/1a557a0c200d61d4e4fb/raw/e5932f9923e57dcb41db32fe5bed9acc6d584be4/fix_virtualenv)
  • via wget
sh <(wget -qO- https://gist.github.com/tevino/1a557a0c200d61d4e4fb/raw/e5932f9923e57dcb41db32fe5bed9acc6d584be4/fix_virtualenv)

@laymonk
Copy link

laymonk commented Oct 6, 2016

I stumbled upon this, and just had to comment.

I imagine you reckon you are being helpful, but this a most dangerous thing to ask people to run. Running it could easily delete all links in /usr/local, which is the standard place homebrew installs pip (so, your $ENV_PATH will be /usr/local)

The simple fix involves just cd'ing into the virtualenv, and deleting all the stale links to the upgraded python by running:
find . -type l -delete

.. and then re-installing your virtualenv:
mkvirtualenv venv-name or virtualenv venv-name

An unsuspecting user may not understand the implication of answering yes, and have all symlinks blown away from /usr/local .. I think you should take this down, or amend it. No offence intended.

@richardsalex
Copy link

@laymonk, thanks for this.

@ttimasdf
Copy link

ttimasdf commented Nov 2, 2017

I think it may be better if we add an option for user to choose which python executable to use, e.g. python3.

8a9,11
> PYEXC_DEF="$(sed -n '1s/^#!//p' $SYSTEM_VIRTUALENV)"
> read -p "  Choose which python to use? [$PYEXC_DEF] " PYEXC
> echo
13c16
<     $SYSTEM_VIRTUALENV "$ENV_PATH"
---
>     $SYSTEM_VIRTUALENV "$ENV_PATH" -p ${PYEXC:-$PYEXC_DEF}
15c18
< fi
\ No newline at end of file
---
> fi

I want to add an 🐍 python emoji in prompt but Github doesn't allow me to 😞

For ease, run my patched version here:

sh <(curl -sL https://gist.github.com/ttimasdf/34bc85ac008f68b2ee098850cab2979c/raw/56143d3301322136714e429079c164664f603945/fix_virtualenv)

@puneetkaura
Copy link

Thanks @ttimasdf used your script saved me an hour of work.

@tevino
Copy link
Author

tevino commented Apr 22, 2018

Hi @laymonk, I know it's been years but I just saw this, the extra logic is added to prevent damaging /usr/local, your credit was added(I'll remove it if you are not OK with it), I'm sorry for your loss.

@itcomputer12
Copy link

I‘ve gone directly into the pit! This is about a resolution about "delete all links in /usr/local". For reference only,url is here,http://www.cnblogs.com/itcomputer/p/8984828.html.

@Raisins
Copy link

Raisins commented Nov 29, 2018

thank you soo much @tevino due to a brew install mistake. I spent all day fixing my python, virtualenv and this was the last piece. I was not looking forward to trying to fix all those symlinks for all my projects.

@luisrock
Copy link

luisrock commented Jan 7, 2019

Thanks, man. Perfect solution!

@abdul-hamid-achik
Copy link

man you just saved me from spending 2 more hours thank you very much

@bastula
Copy link

bastula commented Apr 10, 2019

This script is awesome. Thank you for posting it. Way better than my hunt and peck method.

@bolinocroustibat
Copy link

bolinocroustibat commented Aug 2, 2019

The script switched the venv from Python 3 to Python 2.7 on my machine, so I had to delete and rebuild the virtualenv. Might be good to use only python3.

@LizhangX
Copy link

Works like a charm! Thank you!

@rhaines-chwy
Copy link

Works perfectly! Thanks!

@eoglethorpe
Copy link

yep this is magical, thank you

@alexruimy
Copy link

Thank you!

@vsergeyev
Copy link

Thanks!

@vavasilva
Copy link

Thank you!

@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