10

I recently ran (OS X 10.6.8) brew update and brew upgradeand am working in a virtualenv that now fails. I've tried:

$ brew unlink python && brew link python
Unlinking /usr/local/Cellar/python/2.7.8_1... 38 symlinks removed
Linking /usr/local/Cellar/python/2.7.8_1... 35 symlinks created

But in the virtualenv, I still get this:

$ python --version
dyld: Library not loaded: @executable_path/../.Python
  Referenced from: /Users/admin/.virtualenvs/saves/bin/python
  Reason: image not found
Trace/BPT trap

I've found another post on this, but the solution still seems unclear. I'm in a hurry, so uninstalled the Python version and ran brew doctor which returned the error above as a warning:

sh: line 1: 40991 Trace/BPT trap          python -V 2>&1

Then brew install python. Still getting the above error in the virtualenv.

Related questions and a post on debugfix aren't definitive:

Broken references in Virtualenvs
dyld-library-loaded-executable_path-python

Update

Creating a new virtualenv solved this specific problem.

Community
  • 1
  • 1
Dave Everitt
  • 17,193
  • 6
  • 67
  • 97
  • 1
    Your question doesn't say whether you tried to create a new virtual environment, and whether that fails. Possibly with `pip freeze` as suggested in your second supplied link. –  Sep 25 '14 at 08:57
  • I did end up creating a new virtualenv after all the above, and that solved the problem. The old virtualenv broke after the brew upgrade. Now because a dependency of the Pyramid app i'm running wants Fortran on the system itself I have other issues, which took ages to solve the first time :-( – Dave Everitt Sep 25 '14 at 09:52
  • 1
    The related links tell you exactly that, though: because virtualenv creates symbolic links to files, and homebrew replaces those files when upgrading with differently named files, you end up with broken links. Both links gave suggestions how to solve that: one to create a new virtualenv, the other to fix the broken links. All in all, when Python upgrades again in homebrew, you may end up with the same situation. –  Sep 25 '14 at 11:17

2 Answers2

17

I'll put my comment as an answer, for clarity for future visitors with the same problem.

The related links you gave tell you exactly what's the problem: because virtualenv creates symbolic links to files, and homebrew replaces those files when upgrading with differently named files, you end up with broken links. Both related links give suggestions how to solve that: one to create a new virtualenv, the other to fix the broken links.

All in all though, when Python upgrades again in homebrew, you may end up with the same situation.

This was apparently realised by the virtualenv developers, and there is an option --always-copy to avoid problems like this:

$ virtualenv --help
Usage: virtualenv-3.4 [OPTIONS] DEST_DIR

Options:
  ...

  --always-copy         Always copy files rather than symlinking.

  ...

This should prevent problems when upgrading Python through Homebrew in the future. Though then, of course, your virtualenv will have an older version of Python. Which may at times be exactly what you want.

2

Use something like this:

find <virtualenv> -type l -delete
virtualenv -p $(which python3) <virtualenv>

All of the symlinks are broken, so it's necessary to delete them. When you recreate the virtualenv it recreates the symlinks while keeping installed packages.

Note: Replace $(which python3) with location of whatever python version you want to use.

drice304
  • 96
  • 1
  • 3