Python Virtualenvs 中损坏的引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23233252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Broken references in Virtualenvs
提问by oxtay
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:
我最近在我的 Mac 上安装了一堆 dotfiles 以及其他一些应用程序(我改为 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
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)?
我已经删除了与 dotfiles 相关的所有文件,并将我的 .bash_profile 恢复到以前的状态,但问题仍然存在。有什么方法可以诊断问题或以简单的方式解决问题(例如,不需要重新创建所有虚拟环境)?
采纳答案by Ryan Kaskel
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.
要点是,当您创建 virtualenv 时,会为 Homebrew 安装的 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).
当您使用 Homebrew 升级 Python 然后运行时brew cleanup,virtualenv 中的符号链接指向不再存在的路径(因为 Homebrew 删除了它们)。
The symlinks needs to point to the newly installed Python:
符号链接需要指向新安装的 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:
解决方案是删除 virtualenv 中的符号链接,然后重新创建它们:
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:
在我看来,最好只删除损坏的符号链接。您可以使用 GNU 执行此操作find:
gfind ~/.virtualenvs/my-virtual-env/ -type l -xtype l -delete
You can install GNU findwith Homebrew if you don't already have it:
find如果您还没有安装 GNU ,可以使用 Homebrew安装它:
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 findbinary that ships with OS X.
请注意,默认情况下,随 Homebrew 安装的 GNU 程序往往以字母 为前缀g。这是为了避免隐藏findOS X 附带的二进制文件。
回答by Nate
It appearsthe proper way to resolve this issue is to run
看来解决这个问题的正确方法是运行
pip install --upgrade virtualenv
after you have upgraded python with Homebrew.
使用 Homebrew 升级 python 之后。
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 pythonand pipand easy_installand virtualenvand 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.
这应该是任何安装诸如 python 之类的东西的通用程序,它有自己的包管理系统。当您安装brew install python,在安装python和pip和easy_install和virtualenv等。因此,如果这些工具可以自我更新,那么最好在将 Homebrew 视为问题根源之前尝试这样做。
回答by Robert Brisita
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.
这发生在我从 Snow Leopard 更新到 Mac OS X Mavericks 时。我也必须事先重新安装 brew。希望您使用 pip 为您的项目运行了 freeze 命令。
To resolve, you have to update the paths that the virtual environment points to.
要解决,您必须更新虚拟环境指向的路径。
- Install a version of python with brew:
- 使用 brew 安装 python 版本:
brew install python
brew install python
- Re-install virtualenvwrapper.
- 重新安装 virtualenvwrapper。
pip install --upgrade virtualenvwrapper
pip install --upgrade virtualenvwrapper
- Removed the old virtual environment:
- 删除旧的虚拟环境:
rmvirtualenv old_project
rmvirtualenv old_project
- Create a new virtual environment:
- 创建一个新的虚拟环境:
mkvirtualenv new_project
mkvirtualenv new_project
- Work on new virtual environment
- 在新的虚拟环境中工作
workon new_project
workon new_project
- Use pip to install the requirements for the new project.
- 使用 pip 安装新项目的需求。
pip install -r requirements.txt
pip install -r requirements.txt
This should leave the project as it was before.
这应该使项目保持原样。
回答by Chris Wedgwood
After trying a few things, this worked for me:
尝试了几件事后,这对我有用:
go to your virtualenv directory (but don't run workon):
转到您的 virtualenv 目录(但不要运行 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:
然后重建你的venv,运行:
virtualenv .
workon name_of_broken_venv
pip freeze
You should now see a list of your installed packages again.
您现在应该再次看到已安装软件包的列表。
回答by WeizhongTu
A update version @Chris Wedgwood's answer for keeping site-packages(keeping packages installed)
更新版本@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/
回答by sds
The accepted answer does not work for me: the file $WORKON_HOME/*/bin/python2.7is no longer a symlink, it is a full-fledged executable:
接受的答案对我不起作用:该文件$WORKON_HOME/*/bin/python2.7不再是符号链接,而是一个成熟的可执行文件:
$ 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 allthe 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
回答by Ayush Goel
Using Python 2.7.10.
使用 Python 2.7.10。
A single command virtualenv path-to-envdoes it. documentation
一个命令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.
回答by jmoz
If you've busted python3 just try brew upgrade python3that fixed it for me.
如果您已经破解了 python3,请尝试brew upgrade python3为我修复它。
回答by ryan
If this was caused by a brew upgradethat 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.
如果这是由brew upgrade升级其 Python 的引起的,并且您可以降级到以前的版本,请尝试brew switch python [previous version],例如brew switch python 3.6.5。从这里。
回答by thekosmix
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 awsexecutable is pointing towards is either doesn't exist or is corrupted, thus I uninstalled and reinstalled aws-clifollowing instructions from this linkand it worked!!
我最近遇到了这个。以上解决方案都不适合我。似乎这实际上不是 Python 的问题。当我运行时,aws s3 ls
我收到以下错误:dyld: Library not loaded: @executable_path/../.Python
这意味着,库aws可执行文件指向的要么不存在要么已损坏,因此我aws-cli按照此链接中的说明卸载并重新安装了它,并且它起作用了!

