Broken Links in Virtualenvs

I recently installed a bunch of dotfiles on my Mac along with some other applications (I switched to iTerm instead of Terminal and Sublime as the default text editor), but since then all my virtual environments have stopped working, although their folders inside .virtualenvs are still there and they give the following error when I try to run something 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 deleted all the files related to dotfiles and restored my .bash_profile to what it was before, but the problem persists. Is there a way to diagnose a problem or solve it in a simple way (for example, you do not need to create all virtual virtual networks again)?

+186
python homebrew virtualenv dyld macos
Apr 23 '14 at 1:30
source share
16 answers

I found a solution to the problem here , so all loans belong to the author.

The bottom line is that when you create virtualenv, many symbolic links are created on Homebrew installed by 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 virtualenv point to paths that no longer exist (since Homebrew deleted them).

Symbolic links should point to 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 delete the symlinks in virtualenv and then recreate them:

 find ~/.virtualenvs/my-virtual-env/ -type l -delete virtualenv ~/.virtualenvs/my-virtual-env 

It is probably best to check which links will be removed before deleting them:

 find ~/.virtualenvs/my-virtual-env/ -type l 

In my opinion, it’s even better to remove only broken symbolic links. You can do this with GNU find :

 gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete 

You can install GNU find using Homebrew if you do not already have it:

 brew install findutils 

Note that by default, GNU programs installed with Homebrew are prefixed with the letter g . This is to avoid obscuring the find binary that comes with OS X.

+310
Sep 20 '14 at 9:31
source share

After trying a few things, this worked for me:

go to virtualenv directory (but don't start 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 installed packages.

+34
Apr 30 '15 at 14:02
source share

This happened when I upgraded to Mac OS X Mavericks from Snow Leopard. I also had to pre-install brew. I hope you launched the freeze command for your project with pip.

To solve the problem, you need to update the paths that the virtual environment points to.

  • Install python version using brew:

brew install python

  • Reinstall virtualenvwrapper.

pip install --upgrade virtualenvwrapper

  • Removed the old virtual environment:

rmvirtualenv old_project

  • Create a new virtual environment:

mkvirtualenv new_project

  • Work with the new virtual environment

workon new_project

  • Use pip to set requirements for a new project.

pip install -r requirements.txt

This should leave the project as it was before.

+12
Aug 15 '14 at 20:24
source share

It seems the right way to solve this problem is to run

  pip install --upgrade virtualenv 

after you have updated Python using Homebrew.

This should be a general procedure for any formula that installs something like python, which has its own package management system. When you install brew install python , you install python pip easy_install virtualenv and so on. Thus, if these tools can be self-updating, it is best to try to do this before considering Homebrew as a source of problems.

+7
May 14 '14 at 16:18
source share

@Chris Wedgwood update version response to save site-packages (install packages)

 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/ 
+5
Jun 30 '16 at 2:58
source share

If this was caused by a brew upgrade that upgraded its Python, and you agree with the brew switch python [previous version] to the previous version, try brew switch python [previous version] , for example, brew switch python 3.6.5 . From here.

+5
Jul 03 '18 at 0:06
source share

If you have ruined python3, just try brew upgrade python3 , which fixed it for me.

+2
Dec 13 '17 at 11:41 on
source share

I recently came across this. None of the above solutions helped me. This didn't seem to be a Python issue. When i was running

aws s3 ls

I get the following error:

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

This means that the aws library aws indicates that it does not exist or is corrupted, so I uninstalled and reinstalled aws-cli following the instructions on this link, and it worked !!

+2
Sep 19 '18 at 6:20
source share

virtualenvwrapper instructions

As indicated in the accepted answer, the main reason is most likely the home update, which means that your virtualenv symlinks point to the wrong python paths - see details here .

For each virtual env, you need to reassign symbolic links so that they point to the correct path to the python (in the brew basement). Here's how to do it with virtualenvwrapper . Here I am updating the en-my-example-env virtual environment.

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

Done.

+2
Feb 10 '19 at 12:24
source share

Using Python 2.7.10.

Only one virtualenv path-to-env command is used. 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. 
+1
Oct 13 '17 at 8:05
source share

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

We can check and fix it.

 >> 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 
+1
Mar 22 '19 at 16:54
source share

Anyone using pipenv (and you should!) Can simply use these two commands - without activating venv:

 rm -rf 'pipenv --venv' # remove the broken venv pipenv install --dev # reinstall the venv from pipfile 
+1
May 15 '19 at 17:37
source share

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

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

The solution, alas, is to completely remove and recreate all virtual environments from scratch.

For 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 
0
Oct 11 '16 at 2:08 on
source share

Just updating python3 worked for me:

 brew upgrade python3 
0
Jan 17 '19 at 8:29
source share

For me, it was just removing python3 and pipenv and then reinstalling them.

 brew uninstall python3 brew uninstall pipenv brew install python3 brew install pipenv 
0
Mar 05 '19 at 5:59
source share

I tried some of the best methods, but they did not work, for me, which tried to make the toxins work. What ultimately worked:

 sudo pip install tox 

even if the tox has already been established. Exit ends:

 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 
0
May 08 '19 at 16:45
source share



All Articles