Broken references in Virtualenvs

前端 未结 26 1096
一整个雨季
一整个雨季 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:13

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

    0 讨论(0)
  • 2020-11-27 09:16

    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.

    0 讨论(0)
  • 2020-11-27 09:17

    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.

    0 讨论(0)
  • 2020-11-27 09:19

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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 09:22

    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
    
    0 讨论(0)
提交回复
热议问题