Skip to content

Instantly share code, notes, and snippets.

@pv8 pv8/fix-venv.sh
Last active Dec 11, 2019

Embed
What would you like to do?
Fix virtualenv symlinks after upgrading python with Homebrew and running brew cleanup
#!/usr/bin/env bash
#
# Fix virtualenv symlinks after upgrading python with Homebrew and then running
# `cleanup`.
#
# After upgrading Python using Homebrew and then running `brew cleanup` one can
# get this message while trying to run python:
# dyld: Library not loaded: @executable_path/../.Python
# Referenced from: /Users/pablo/.venv/my-app/bin/python
# Reason: image not found
# Trace/BPT trap: 5
#
# This happens because the symlinks in the virtualenv point to paths that no
# longer exist. This script is intended to fix that.
if [[ "$1" == "" ]]; then
echo "usage:"
echo -e "\t./$(basename "$0") <VIRUTALENV-DIR>"
echo "example:"
echo -e "\t./$(basename "$0") \$HOME/.virtualenvs"
exit 0
fi
VENVS_DIR="$1"
# check if GNU find is installed
if [[ ! "$(type -P gfind)" ]]; then
brew install findutils
fi
for entry in $(ls $VENVS_DIR | xargs -n 1 basename); do
venv_name=$(basename "$entry")
if [[ -d $VENVS_DIR/$venv_name ]]; then
# check if Library symlink is broken
if [ -f $VENVS_DIR/$venv_name/.Python ] && [ "$(gfind $VENVS_DIR/$venv_name/.Python -type l -xtype l)" == "" ]; then
echo "== Virtualenv [$venv_name] is ok. Path: $VENVS_DIR/$venv_name"
else
echo "== Virtualenv [$venv_name] has broken symlinks. Path: $VENVS_DIR/$venv_name"
echo -e "\tFixing [$venv_name]"
echo -e "\tRemoving old symlinks..."
echo -e "\tgfind $VENVS_DIR/$venv_name -type l -xtype l -delete"
gfind $VENVS_DIR/$venv_name -type l -xtype l -delete
python_symlink=$(readlink $VENVS_DIR/$venv_name/bin/python)
python_version=$(echo $python_symlink | sed 's/python//g')
echo -e "\tUpdating [$venv_name]..."
if [[ $python_version == 3.* ]]; then
echo -e "\tvirtualenv --python=$(which python3) $VENVS_DIR/$venv_name"
/usr/local/bin/virtualenv --python=$(which python3) $VENVS_DIR/$venv_name
else
echo -e "\tvirtualenv $VENVS_DIR/$venv_name"
/usr/local/bin/virtualenv $VENVS_DIR/$venv_name
fi
fi
fi
done
@pv8

This comment has been minimized.

Copy link
Owner Author

pv8 commented Sep 15, 2016

Running locally:

$ curl -s https://gist.githubusercontent.com/pv8/b04cc66a66a4d156e31d81414046cef8/raw/51e903b72c7d5e1d6d11b389803e89198c3f92aa/fix-venv.sh | bash /dev/stdin <VIRTUALENV-DIR>
@porterjamesj

This comment has been minimized.

Copy link

porterjamesj commented Oct 2, 2016

thank you! this is exactly what i was looking for, mostly because i was too lazy to write it myself—I'm glad I didn't have to :P

@pv8

This comment has been minimized.

Copy link
Owner Author

pv8 commented Oct 15, 2016

Glad it helped! 👍

@chlab

This comment has been minimized.

Copy link

chlab commented Oct 9, 2017

Thanks for this. I'm still getting

/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper

when I activate a virtualenv. Any ideas?

@pv8

This comment has been minimized.

Copy link
Owner Author

pv8 commented Jan 18, 2018

@chlab, have you tried to reinstall virtualenvwrapper?

$ pip install -U virtualenvwrapper
@stranljip

This comment has been minimized.

Copy link

stranljip commented Feb 25, 2019

@chlab - for me it helped to explicitly uninstall and reinstall virtualenv as said in
https://stackoverflow.com/a/21071358:

pip3 uninstall virtualenv
pip3 install virtualenv
@AminAbdisamad

This comment has been minimized.

Copy link

AminAbdisamad commented Mar 7, 2019

Thank You, Your script is very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.