[python] 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)?

This question is related to python macos virtualenv homebrew dyld

The answer is


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/

So there are many ways but one which worked for me is as follows since I already had my requirements.txt file freeze.

So delete old virtual environment with following command use deactivate cd .. rm -r old_virtual_environment

to install virtualenv python package with pip use pip install virtualenv

then check if it's installed correctly use virtualenv --version

jump to your project directory use cd project_directory

now create new virtual environment inside project directory using following use virtualenv name_of_new_virtual_environment

now activate newly created virtual environment use source name_of_new_virtual_environment/bin/activate

now install all project dependencies using following command use pip install -r requirements.txt


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.


I am sure I am late to the party but I want to say that the resolution of this problem is much simpler than discussed here.

You can easily regenerate the virtual environment without having to delete/edit anything. Assuming that your broken environment is called env_to_fix you can just to the following:

mkvirtualenv env_to_fix

This will regenerate the links and fix the environment without the need to dump the current status somewhere and restore it.


Anyone who is using pipenv (and you should!) can simply use these two commands — without having the venv activated:

rm -rf `pipenv --venv` # remove the broken venv
pipenv install --dev   # reinstall the venv from pipfile 

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.


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.


All the answers are great here, I tried a couple of solutions mentioned above by Ryan, Chris and couldn't resolve the issue, so had to follow a quick and dirty way.

  1. rm -rf <project dir> (or mv <project dir> <backup projct dir> if you want to keep a backup)
  2. git clone <project git url>
  3. Move on!

Nothing novel here, but it makes life easier!


Virtualenvs are broken. Sometimes simple way is to delete venv folders and recreate virutalenvs.


I tried the top few methods, but they didn't work, for me, which were trying to make tox work. What eventually worked was:

sudo pip install tox

even if tox was already installed. The output terminated with:

Successfully built filelock
Installing collected packages: py, pluggy, toml, filelock, tox
Successfully installed filelock-3.0.10 pluggy-0.11.0 py-1.8.0 toml-0.10.0 tox-3.9.0

Simply upgrading python3 worked for me:

brew upgrade python3

virtualenvwrapper instructions

As indicated in the accepted answer, the root cause is likely a homebrew update that means your virtualenv symlinks are pointing at broken python paths - see details here.

For each virtual env, you need to reassign the symlinks to point at the correct python path (in brew cellar). Here is how to do it with virtualenvwrapper. Here I am updating a virtual env called "my-example-env".

cd ~/PYTHON_ENVS
find ./my-example-env -type l -delete
mkvirtualenv my-example-env

All done.


Using Python 2.7.10.

A single command virtualenv path-to-env does it. documentation

$ virtualenv path-to-env
Overwriting path-to-env/lib/python2.7/orig-prefix.txt with new content
New python executable in path-to-env/bin/python2.7
Also creating executable in path-to-env/bin/python
Installing setuptools, pip, wheel...done.

If you using pipenv, just doing pipenv --rm solves the problem.


I recently faced this. None of the above solutions worked for me. Seems it wasn't actually Python's problem. When I was running

aws s3 ls

I was getting following error:

dyld: Library not loaded: @executable_path/../.Python

This means, the library aws executable is pointing towards is either doesn't exist or is corrupted, thus I uninstalled and reinstalled aws-cli following instructions from this link and it worked!!


If this was caused by a brew upgrade that upgraded its Python, and you're ok with downgrading to the previous version, try brew switch python [previous version], eg brew switch python 3.6.5. From here.


If you've busted python3 just try brew upgrade python3 that fixed it for me.


I was facing the same issue after upgrading brew on my OSX Catalina.

After trying bunch of stuffs, I find the following is the best and easy solution.

At first, delete the virtual env. (Optional)

find myvirtualenv -type l -delete

then recreate a new virtualenv

virtualenv myvirtualenv

Reference: https://www.jeremycade.com/python/osx/homebrew/2015/03/02/fixing-virtualenv-after-a-python-upgrade/


When you are running into this issue on a freshly created virtualenv, it might be that your python version installed by brew is "unlinked".

You can fix this for example by running: brew link [email protected] (but specify your speficic python version)

You can also run brew doctor, it will tell you if you have unlinked stuff and how to fix this.


The problem for me(a MacOS user) is that brew updated the Python and virtualenvs links to the old version which was deleted.

We can check and fix it by

>> ls -al ~/.virtualenvs/<your-virtual-env>/.Python
.Python -> /usr/local/Cellar/python/<old-version>/Frameworks/Python.framework/Versions/3.7/Python
>> rm ~/.virtualenvs/<your-virtual-env>/.Python
>> ln -s  /usr/local/Cellar/python/<new-version>/Frameworks/Python.framework/Versions/3.7/Python ~/.virtualenvs/<your-virtual-env>/.Python

The accepted answer does not work for me: the file $WORKON_HOME/*/bin/python2.7 is no longer a symlink, it is a full-fledged executable:

$ file $WORKON_HOME/*/bin/python2.7
/Users/sds/.virtualenvs/.../bin/python2.7: Mach-O 64-bit executable x86_64
...

The solution is, alas, to completely remove and re-create from scratch all the virtual environments.

For the reference:

deactivate
pip install --user virtualenv virtualenvwrapper
pip install --user --upgrade virtualenv virtualenvwrapper
for ve in $(lsvirtualenv -b); do
  # assume that each VE is associated with a project
  # and the project has the requirements.txt file
  project=$(cat $WORKON_HOME/$ve/.project)
  rmvirtualenv $ve
  mkvirtualenv -a $project -r requirements.txt $ve
done

I came across the same issue when I was pointing my python run time from 2 to 3 on my mac, pointing the alias python to python 3 path. I then recreate a new virtualenv and re-install those packages i need for my project. For my use case i have had a python program writing to google sheet. Clean up a few packages that are different from python 2 implementation and wa la, things started working again.


I had a similar issue and i solved it by just rebuilding the virtual environment with virtualenv .


I had a broken virtual env due to a Homebrew reinstall of python (thereby broken symlinks) and also a few "sudo pip install"s I had done earlier. Weizhong's tips were very helpful in fixing the issues without having to reinstall packages. I also had to do the following for the mixed permissions problem.

sudo chown -R my_username lib/python2.7/site-packages


What fixed it for me was just uninstalling python3 and pipenv then reinstalling them.

brew uninstall pipenv
brew uninstall python3
brew install python3 
brew install pipenv

Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to virtualenv

How to setup virtual environment for Python in VS Code? Conda version pip install -r requirements.txt --target ./lib What is the purpose of "pip install --user ..."? What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc? ImportError: No module named 'encodings' how to specify new environment location for conda create Use virtualenv with Python with Visual Studio Code in Ubuntu Is it ok having both Anacondas 2.7 and 3.5 installed in the same time? How to uninstall a package installed with pip install --user Unable to install boto3

Examples related to homebrew

dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac How can I install a previous version of Python 3 in macOS using homebrew? SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 pip3: command not found env: node: No such file or directory in mac Stuck at ".android/repositories.cfg could not be loaded." Brew install docker does not include docker engine? What do raw.githubusercontent.com URLs represent? Homebrew refusing to link OpenSSL

Examples related to dyld

Reason: no suitable image found dyld: Library not loaded: @rpath/libswiftCore.dylib OS X Framework Library not loaded: 'Image not found' registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta Broken references in Virtualenvs dyld: Library not loaded: /usr/local/lib/libpng16.16.dylib with anything php related dyld: Library not loaded ... Reason: Image not found