Virtualenvs中的参考损坏

问题:Virtualenvs中的参考损坏

我最近在Mac上安装了许多点文件以及其他一些应用程序(我改为使用iTerm而不是Terminal,将Sublime设置为默认文本编辑器),但是此后,尽管它们的文件夹位于.virtualenvs中,但我所有的虚拟环境都停止了工作仍然在那里,每当我尝试在其中运行任何命令时,它们都会给出以下错误:

dyld: Library not loaded: @executable_path/../.Python
  Referenced from: /Users/[user]/.virtualenvs/modclass/bin/python
  Reason: image not found
Trace/BPT trap: 5

我已经删除了所有与dotfiles相关的文件,并将.bash_profile还原到以前的状态,但是问题仍然存在。是否有任何方法可以诊断问题或以简单的方式解决问题(例如,无需再次创建所有虚拟环境)?

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)?


回答 0

我在这里找到了解决问题的方法,所以所有功劳都归功于作者。

要点是,当您创建一个virtualenv时,会为安装了Homebrew的Python创建许多符号链接。

这是一个例子:

$ 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
...

当您使用Homebrew升级Python然后运行时brew cleanup,virtualenv中的符号链接指向不再存在的路径(因为Homebrew删除了它们)。

符号链接需要指向新安装的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

解决方案是删除virtualenv中的符号链接,然后重新创建它们:

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

最好在删除链接之前先检查哪些链接将被删除:

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

我认为,最好只删除损坏的符号链接。您可以使用GNU执行此操作find

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

find如果尚未安装GNU ,可以使用Homebrew 进行安装:

brew install findutils

请注意,默认情况下,随Homebrew一起安装的GNU程序通常以字母开头g。这是为了避免遮盖findOS X附带的二进制文件。

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.


回答 1

在尝试了几件事之后,这对我有用:

转到您的virtualenv目录(但不要运行workon):

cd ~/.virtualenv/name_of_broken_venv

现在删除这些文件:

rm -rf .Python bin/python* lib/python2.7/* include/python2.7

然后重建您的venv,运行:

virtualenv .
workon name_of_broken_venv
pip freeze

现在,您应该再次看到已安装软件包的列表。

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.


回答 2

当我从Snow Leopard更新到Mac OS X Mavericks时,就会发生这种情况。我也必须事先重新安装brew。希望您使用pip为项目运行了冻结命令。

若要解决,您必须更新虚拟环境指向的路径。

  • 使用brew安装python版本:

brew install python

  • 重新安装virtualenvwrapper。

pip install --upgrade virtualenvwrapper

  • 删除了旧的虚拟环境:

rmvirtualenv old_project

  • 创建一个新的虚拟环境:

mkvirtualenv new_project

  • 在新的虚拟环境上工作

workon new_project

  • 使用pip安装新项目的要求。

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.


回答 3

@Chris Wedgwood保留更新版本的答案site-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/

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/

回答 4

看来解决此问题的正确方法是运行

 pip install --upgrade virtualenv

用Homebrew升级python之后。

对于安装类似python的任何公式,该公式应该是一个通用过程,它具有自己的包管理系统。当您安装brew install python,在安装pythonpipeasy_installvirtualenv等。因此,如果这些工具可以自我更新,那么最好先尝试这样做,然后再将“自制”视为问题的根源。

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.


回答 5

如果这是由brew upgrade升级其Python 引起的,并且可以降级到以前的版本,请尝试brew switch python [previous version],例如brew switch python 3.6.5从这里。

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.


回答 6

virtualenvwrapper指令

如已接受的答案所示,根本原因可能是自制程序更新,这意味着您的virtualenv符号链接指向断开的python路径-请在此处查看详细信息。

对于每个虚拟环境,您需要重新分配符号链接以指向正确的python路径(在Brew酒窖中)。这是使用virtualenvwrapper进行操作的方法。在这里,我正在更新一个名为“ my-example-env”的虚拟环境。

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

全做完了。

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.


回答 7

任何使用pipenv的人(并且应该使用!)都可以简单地使用以下两个命令-无需激活venv:

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

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 

回答 8

如果您破坏了python3,请尝试brew upgrade python3为我修复它。

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


回答 9

我最近遇到了这个问题。以上解决方案均不适合我。看来这实际上不是Python的问题。当我运行

aws s3 ls

时,出现以下错误:

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

这意味着库aws可执行文件指向的是不存在或已损坏,因此我aws-cli按照此链接的说明进行了卸载并重新安装,并且有效!

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!!


回答 10

我(MacOS用户)遇到的问题是,brew将Python和virtualenvs链接更新为已删除的旧版本。

我们可以通过以下方式进行检查和修复

>> 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 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

回答 11

我有一个类似的问题,我只是通过使用 virtualenv .

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


回答 12

使用Python 2.7.10。

一个命令即可virtualenv path-to-env完成。文件资料

$ 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.

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.

回答 13

由于python的Homebrew重新安装(因此损坏的符号链接)以及我之前完成的一些“ sudo pip安装”,我的虚拟环境也损坏了。Weizhong的技巧对解决问题非常有用,而无需重新安装软件包。对于混合权限问题,我还必须执行以下操作。

须藤chown -R my_username lib / python2.7 / site-packages

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


回答 14

Virtualenvs已损坏。有时,简单的方法是删除venv文件夹并重新创建virutalenvs。

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


回答 15

如果您使用pipenv,只需pipenv --rm解决即可。

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


回答 16

在OSX Catalina上升级brew后,我遇到了同样的问题。

在尝试了一堆东西之后,我发现以下是最佳和简便的解决方案。

首先,删除虚拟环境。(可选的)

find myvirtualenv -type l -delete

然后重新创建一个新的virtualenv

virtualenv myvirtualenv

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

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/


回答 17

接受的答案对我不起作用:该文件$WORKON_HOME/*/bin/python2.7不再是符号链接,而是功能全面的可执行文件:

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

a,解决方案是完全删除所有虚拟环境并从头开始重新创建。

供参考:

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

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

回答 18

简单地升级python3对我有用:

brew upgrade python3

Simply upgrading python3 worked for me:

brew upgrade python3

回答 19

我尝试了前几种方法,但对我而言,它们却无济于事,这些方法正试图使毒素发挥作用。最终有效的是:

sudo pip install tox

即使已经安装了tox。输出终止于:

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

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

回答 20

对我来说,解决此问题的方法只是卸载python3和pipenv,然后重新安装它们。

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

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

回答 21

这里所有的答案都很棒,我尝试了Ryan,Chris上面提到的几种解决方案,但无法解决问题,因此必须采取快速而肮脏的方法。

  1. rm -rf <project dir>(或者mv <project dir> <backup projct dir>如果您想保留备份)
  2. git clone <project git url>
  3. 继续!

这里没有什么新奇的东西,但是它使生活更轻松!

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!


回答 22

我确定我晚会晚了,但是我想说,解决这个问题比这里讨论的要简单得多。

您可以轻松地重新生成虚拟环境,而无需删除/编辑任何内容。假设您调用了损坏的环境,则env_to_fix可以执行以下操作:

mkvirtualenv env_to_fix

这将重新生成链接并修复环境,而无需将当前状态转储到某个位置并进行恢复。

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.


回答 23

当我在Mac上将python运行时从2指向3时,遇到了相同的问题,将别名python指向python 3路径。然后,我重新创建一个新的virtualenv并重新安装我的项目所需的那些软件包。对于我的用例,我有一个写给Google工作表的python程序。清理一些与python 2实现不同的程序包,然后一切又开始起作用。

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.