Broken references in Virtualenvs

前端 未结 26 1273
一整个雨季
一整个雨季 2020-11-27 08:53

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 eve

26条回答
  •  野性不改
    2020-11-27 09:21

    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.

提交回复
热议问题