How to fix your virtualenv after an upgrade of Python with Homebrew

Python ?? Comments 29 July 2014

If you are using Homebrew on OSX and Python and there is a small virtualenv then this article is for you.

After a small upgrade of Python via Brew, you risk to get this error when you want to use the virtualenv.

In my case, the directory where I store the virtualenvs is ~/.virtualenvs

$HOME/.virtualenvs/
    pelican/

Loads the virtualenv.

$HOME/.virtualenvs (0) > source ~/.virtualenvs/pelican/bin/activate.sh

$HOME/.virtualenvs (0) > (pelican) pelican
dyld: Library not loaded: @executable_path/../.Python
  Referenced from: $HOME/.virtualenvs/pelican/bin/python
  Reason: image not found
fish: Job 1, 'pelican' terminated by signal SIGTRAP (Trace or breakpoint trap)

$HOME/.virtualenvs (0) > (pelican) deactivate

In fact, there is a problem with the library, you need to regenerate the links of the Python interpreter. In this case, this is just to remove the existing links and re-execute the virtualenv command.

$HOME/.virtualenvs (0) > find pelican/ -type l
pelican/.Python
pelican/bin/python
pelican/bin/python2
pelican/include/python2.7
pelican/lib/python2.7/_abcoll.py
pelican/lib/python2.7/_weakrefset.py
pelican/lib/python2.7/abc.py
pelican/lib/python2.7/codecs.py
pelican/lib/python2.7/config
pelican/lib/python2.7/copy_reg.py
pelican/lib/python2.7/encodings
pelican/lib/python2.7/fnmatch.py
pelican/lib/python2.7/genericpath.py
pelican/lib/python2.7/lib-dynload
pelican/lib/python2.7/linecache.py
pelican/lib/python2.7/locale.py
pelican/lib/python2.7/ntpath.py
pelican/lib/python2.7/os.py
pelican/lib/python2.7/posixpath.py
pelican/lib/python2.7/re.py
pelican/lib/python2.7/sre.py
pelican/lib/python2.7/sre_compile.py
pelican/lib/python2.7/sre_constants.py
pelican/lib/python2.7/sre_parse.py
pelican/lib/python2.7/stat.py
pelican/lib/python2.7/types.py
pelican/lib/python2.7/UserDict.py
pelican/lib/python2.7/warnings.py

All the symlinks are wrong, and we need to remove them and re-execute the virtualenv command.

$HOME/.virtualenvs (0) > find pelican/ -type l -delete

$HOME/.virtualenvs (0) > virtualenv pelican
Overwriting pelican/lib/python2.7/site.py with new content
Overwriting pelican/lib/python2.7/orig-prefix.txt with new content
New python executable in pelican/bin/python2.7
Also creating executable in pelican/bin/python
Installing setuptools, pip...done.
Overwriting pelican/bin/activate_this.py with new content

Loads the virtualenv ;-)

$HOME/.virtualenvs (0) > source ~/.virtualenvs/pelican/bin/activate.sh

$HOME/.virtualenvs > (pelican) pelican --version
3.5.dev

And now, you have a working virtualenv.

Tags: python virtualenv homebrew