Broken references in Virtualenvs
I recently installed a bunch of dotfiles on my Mac along with some other applications (I changed to iTerm instead of Terminal, and Sublime as my default text editor) but ever since, all my virtual environments have stopped working, although their folders inside .virtualenvs are still there and they give the following error whenever I try to run anything in them:
dyld: Library not loaded: @executable_path/../.Python
Referenced from: /Users/[user]/.virtualenvs/modclass/bin/python
Reason: image not found
Trace/BPT trap: 5
I have removed all the files related to dotfiles and have restored my .bash_profile to what it was before, but the problem persists. Is there any way to diagnose the problem or solve it in an easy way (e.g. not requiring to create all the virtualenvs all over again)?
Solution 1:
I found the solution to the problem here, so all credit goes to the author.
The gist is that when you create a virtualenv, many symlinks are created to the Homebrew installed Python.
Here is one example:
$ ls -la ~/.virtualenvs/my-virtual-env
...
lrwxr-xr-x 1 ryan staff 78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.7/Frameworks/Python.framework/Versions/2.7/Python
...
When you upgrade Python using Homebrew and then run brew cleanup
, the symlinks in the virtualenv point to paths that no longer exist (because Homebrew deleted them).
The symlinks needs to point to the newly installed Python:
lrwxr-xr-x 1 ryan staff 78 Jun 25 13:21 .Python -> /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/Python
The solution is to remove the symlinks in the virtualenv and then recreate them:
find ~/.virtualenvs/my-virtual-env/ -type l -delete
virtualenv ~/.virtualenvs/my-virtual-env
It's probably best to check what links will be deleted first before deleting them:
find ~/.virtualenvs/my-virtual-env/ -type l
In my opinion, it's even better to only delete broken symlinks. You can do this using GNU find
:
gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete
You can install GNU find
with Homebrew if you don't already have it:
brew install findutils
Notice that by default, GNU programs installed with Homebrew tend to be prefixed with the letter g
. This is to avoid shadowing the find
binary that ships with OS X.
Solution 2:
After trying a few things, this worked for me:
go to your virtualenv directory (but don't run workon):
cd ~/.virtualenv/name_of_broken_venv
Now delete these files:
rm -rf .Python bin/python* lib/python2.7/* include/python2.7
Then to rebuild your venv, run:
virtualenv .
workon name_of_broken_venv
pip freeze
You should now see a list of your installed packages again.
Solution 3:
This occurred when I updated to Mac OS X Mavericks from Snow Leopard. I had to re-install brew beforehand too. Hopefully you ran the freeze command for your project with pip.
To resolve, you have to update the paths that the virtual environment points to.
- Install a version of python with brew:
brew install python
- Re-install virtualenvwrapper.
pip install --upgrade virtualenvwrapper
- Removed the old virtual environment:
rmvirtualenv old_project
- Create a new virtual environment:
mkvirtualenv new_project
- Work on new virtual environment
workon new_project
- Use pip to install the requirements for the new project.
pip install -r requirements.txt
This should leave the project as it was before.
Solution 4:
A update version @Chris Wedgwood
's answer for keeping site-packages
(keeping packages installed)
cd ~/.virtualenv/name_of_broken_venv
mv lib/python2.7/site-packages ./
rm -rf .Python bin lib include
virtualenv .
rm -rf lib/python2.7/site-packages
mv ./site-packages lib/python2.7/
Solution 5:
It appears the proper way to resolve this issue is to run
pip install --upgrade virtualenv
after you have upgraded python with Homebrew.
This should be a general procedure for any formula that installs something like python, which has it's own package management system. When you install brew install python
, you install python
and pip
and easy_install
and virtualenv
and so on. So, if those tools can be self-updated, it's best to try to do so before looking to Homebrew as the source of problems.