Last active
September 1, 2022 03:33
-
-
Save pv8/b04cc66a66a4d156e31d81414046cef8 to your computer and use it in GitHub Desktop.
Fix virtualenv symlinks after upgrading python with Homebrew and running brew cleanup
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
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@pv8
Thank you for your reply.
i ran all scripts and is also wrong:
sudo pip3 uninstall virtualenv virtualenvwrapper
pip instll virtualenv virtualenvwrapper
export VIRTUALENVWRAPPER_PYTHON=$(which python)
But now it still throws an exception:
``/System/Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.``