-
-
Save pv8/b04cc66a66a4d156e31d81414046cef8 to your computer and use it in GitHub Desktop.
#!/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 |
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
Glad it helped! 👍
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?
@chlab, have you tried to reinstall virtualenvwrapper
?
$ pip install -U virtualenvwrapper
@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
Thank You, Your script is very helpful.
This really should be part of the homebrew install of python to check this, because its deeply frusturating to have homebrew just randomly update python when installing some other app and blam! all your dev environments are now smoking debris. Particularly when theres already a mechanism in pythons best practices for upgrading to new pythons without trashing old pythons
Think you, I ran your script, and output:
==> Downloading https://homebrew.bintray.com/bottles/findutils-4.7.0.big_sur.bottle.tar.gz ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/fae48a197e0a386fd330d8c48b375b5bb289f5d7105f0d007d94c53f7edc060f?response-content-disposition=attachment%3Bfilename%3D%22findutils-4.7.0.big_sur.bo ######################################################################## 100.0% ==> Pouring findutils-4.7.0.big_sur.bottle.tar.gz ==> Caveats All commands have been installed with the prefix "g". If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH from your bashrc like: PATH="/usr/local/opt/findutils/libexec/gnubin:$PATH" ==> Summary 🍺 /usr/local/Cellar/findutils/4.7.0: 32 files, 1.8MB == Virtualenv [Django] has broken symlinks. Path: /Users/ezreal/.virtualenvs/Django Fixing [Django] Removing old symlinks... gfind /Users/ezreal/.virtualenvs/Django -type l -xtype l -delete Updating [Django]... virtualenv /Users/ezreal/.virtualenvs/Django created virtual environment CPython3.9.0.final.0-64 in 789ms creator CPython3Posix(dest=/Users/ezreal/.virtualenvs/Django, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/ezreal/Library/Application Support/virtualenv) added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1 activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator == Virtualenv [qianfeng] has broken symlinks. Path: /Users/ezreal/.virtualenvs/qianfeng Fixing [qianfeng] Removing old symlinks... gfind /Users/ezreal/.virtualenvs/qianfeng -type l -xtype l -delete Updating [qianfeng]... virtualenv /Users/ezreal/.virtualenvs/qianfeng created virtual environment CPython3.9.0.final.0-64 in 356ms creator CPython3Posix(dest=/Users/ezreal/.virtualenvs/qianfeng, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/ezreal/Library/Application Support/virtualenv) added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1 activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
and ran other script:
sudo pip3 install virtualenv virtualenvwrapper
and it is ok;
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.`
my python version is 3.9 and mac python is 2.7
Think you, I ran your script, and output:
==> Downloading https://homebrew.bintray.com/bottles/findutils-4.7.0.big_sur.bottle.tar.gz ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/fae48a197e0a386fd330d8c48b375b5bb289f5d7105f0d007d94c53f7edc060f?response-content-disposition=attachment%3Bfilename%3D%22findutils-4.7.0.big_sur.bo ######################################################################## 100.0% ==> Pouring findutils-4.7.0.big_sur.bottle.tar.gz ==> Caveats All commands have been installed with the prefix "g". If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH from your bashrc like: PATH="/usr/local/opt/findutils/libexec/gnubin:$PATH" ==> Summary 🍺 /usr/local/Cellar/findutils/4.7.0: 32 files, 1.8MB == Virtualenv [Django] has broken symlinks. Path: /Users/ezreal/.virtualenvs/Django Fixing [Django] Removing old symlinks... gfind /Users/ezreal/.virtualenvs/Django -type l -xtype l -delete Updating [Django]... virtualenv /Users/ezreal/.virtualenvs/Django created virtual environment CPython3.9.0.final.0-64 in 789ms creator CPython3Posix(dest=/Users/ezreal/.virtualenvs/Django, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/ezreal/Library/Application Support/virtualenv) added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1 activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator == Virtualenv [qianfeng] has broken symlinks. Path: /Users/ezreal/.virtualenvs/qianfeng Fixing [qianfeng] Removing old symlinks... gfind /Users/ezreal/.virtualenvs/qianfeng -type l -xtype l -delete Updating [qianfeng]... virtualenv /Users/ezreal/.virtualenvs/qianfeng created virtual environment CPython3.9.0.final.0-64 in 356ms creator CPython3Posix(dest=/Users/ezreal/.virtualenvs/qianfeng, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/ezreal/Library/Application Support/virtualenv) added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1 activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
and ran other script:
sudo pip3 install virtualenv virtualenvwrapper
and it is ok;
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.`my python version is 3.9 and mac python is 2.7
@EzrealR, I don't really use virtualenvwrapper
, but I would recommend you to install virtualenv
and virtualenvwrapper
without sudo
and set the VIRTUALENVWRAPPER_PYTHON
to your default Python:
$ deactivate
$ export VIRTUALENVWRAPPER_PYTHON=$(which python)
Think you, I ran your script, and output:
==> Downloading https://homebrew.bintray.com/bottles/findutils-4.7.0.big_sur.bottle.tar.gz ==> Downloading from https://d29vzk4ow07wi7.cloudfront.net/fae48a197e0a386fd330d8c48b375b5bb289f5d7105f0d007d94c53f7edc060f?response-content-disposition=attachment%3Bfilename%3D%22findutils-4.7.0.big_sur.bo ######################################################################## 100.0% ==> Pouring findutils-4.7.0.big_sur.bottle.tar.gz ==> Caveats All commands have been installed with the prefix "g". If you need to use these commands with their normal names, you can add a "gnubin" directory to your PATH from your bashrc like: PATH="/usr/local/opt/findutils/libexec/gnubin:$PATH" ==> Summary 🍺 /usr/local/Cellar/findutils/4.7.0: 32 files, 1.8MB == Virtualenv [Django] has broken symlinks. Path: /Users/ezreal/.virtualenvs/Django Fixing [Django] Removing old symlinks... gfind /Users/ezreal/.virtualenvs/Django -type l -xtype l -delete Updating [Django]... virtualenv /Users/ezreal/.virtualenvs/Django created virtual environment CPython3.9.0.final.0-64 in 789ms creator CPython3Posix(dest=/Users/ezreal/.virtualenvs/Django, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/ezreal/Library/Application Support/virtualenv) added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1 activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator == Virtualenv [qianfeng] has broken symlinks. Path: /Users/ezreal/.virtualenvs/qianfeng Fixing [qianfeng] Removing old symlinks... gfind /Users/ezreal/.virtualenvs/qianfeng -type l -xtype l -delete Updating [qianfeng]... virtualenv /Users/ezreal/.virtualenvs/qianfeng created virtual environment CPython3.9.0.final.0-64 in 356ms creator CPython3Posix(dest=/Users/ezreal/.virtualenvs/qianfeng, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/Users/ezreal/Library/Application Support/virtualenv) added seed packages: pip==20.2.4, setuptools==50.3.2, wheel==0.35.1 activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
and ran other script:
sudo pip3 install virtualenv virtualenvwrapper
and it is ok;
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.
my python version is 3.9 and mac python is 2.7@EzrealR, I don't really use
virtualenvwrapper
, but I would recommend you to installvirtualenv
andvirtualenvwrapper
withoutsudo
and set theVIRTUALENVWRAPPER_PYTHON
to your default Python:$ deactivate $ export VIRTUALENVWRAPPER_PYTHON=$(which python)
@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.``
Running locally: