什么是Python Egg 缓存(PYTHON_EGG_CACHE)?
我刚刚在我的开发机器上从 Python 2.6.1 升级到 2.6.4,启动 python 脚本时出现以下消息:
无法将文件提取到 Egg 缓存
发生以下错误 尝试将文件提取到 Python Egg 缓存:
[Errno 13] 权限被拒绝: '/var/www/.python-eggs'
Python Egg 缓存目录是 当前设置为:
/var/www/.python-eggs
也许您的帐户没有 对此目录的写访问权限?你 可以通过更改缓存目录 设置 PYTHON_EGG_CACHE 环境变量指向 可访问的目录。
python 文档 中没有任何内容所以我对于放置此目录的位置及其用途的最佳实践有点茫然。
有人能解释一下Python的egg缓存是什么吗?
另外,你能解释一下为什么/它与Python用来存储鸡蛋的site-packages
目录不同吗(据我所知)?
I've just upgraded from Python 2.6.1 to 2.6.4 on my development machine and upon starting a python script was presented with the following message:
Can't extract file(s) to egg cache
The following error occurred while
trying to extract file(s) to the
Python egg cache:[Errno 13] Permission denied:
'/var/www/.python-eggs'The Python egg cache directory is
currently set to:/var/www/.python-eggs
Perhaps your account does not have
write access to this directory? You
can change the cache directory by
setting the PYTHON_EGG_CACHE
environment variable to point to an
accessible directory.
There isn't anything in the python docs so I'm at a bit of a loss regarding best-practices on where to put this directory and what it's used for.
Can someone explain what the Python egg cache is?
Also, can you explain why/how it is different to the site-packages
directory Python uses to store eggs (as I understand it)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
根据我的调查,一些 Egg 被打包为 zip 文件,并以这种方式保存在 Python 的
site-packages
目录中。这些压缩的 Eggs 需要先解压才能执行,因此会展开到
PYTHON_EGG_CACHE
目录中,默认情况下为~/.python-eggs
(位于用户的主目录中)目录)。如果不存在,则会在尝试运行应用程序时出现问题。有许多修复方法:
.python-eggs
目录并使其可供用户写入。/tmp/python-eggs
)并将环境变量PYTHON_EGG_CACHE
设置到该目录。easy_install
解压软件包时使用-Z
开关。From my investigations it turns out that some eggs are packaged as zip files, and are saved as such in Python's
site-packages
directory.These zipped eggs need to be unzipped before they can be executed, so are expanded into the
PYTHON_EGG_CACHE
directory which by default is~/.python-eggs
(located in the user's home directory). If this doesn't exist it causes problems when trying to run applications.There are a number of fixes:
.python-eggs
directory in the user's home directory and make it writable for the user./tmp/python-eggs
) and set the environment variablePYTHON_EGG_CACHE
to this directory.-Z
switch when usingeasy_install
to unzip the package when installing.python Egg 缓存只是 setuptools 用来存储符合 egg 规范 的安装包的目录。您可以在此处了解有关 setuptools 的更多信息。
此外,正如错误消息所述,您可以通过设置 PYTHON_EGG_CACHE=/some/other/dir 在您的环境中指定不同的 Egg 缓存目录。最简单的方法是在 ~/.bash_profile 中设置它(假设您使用的是 bash),如下所示:
如果您使用的是 Web 应用程序,则可能需要在 Apache 环境中设置它。
The python egg cache is simply a directory used by setuptools to store packages installed that conform to the egg specification. You can read more about setuptools here.
Additionally, as the error message states, you can specify a different egg cache directory in your environment by setting PYTHON_EGG_CACHE=/some/other/dir. The easiest way to do this is to set it in your ~/.bash_profile (assuming you're using bash), like this:
You may need to set it in your Apache environment if you're using a Web application.
这是使用原本不错的鸡蛋机制的黑暗副作用。
Eggs 是打包到一个
.egg
文件中的包(一个充满文件的目录),以简化部署。它们存储在
/site-packages/
目录中。只要 Egg 中存储的文件是
.py
文件,它就可以很好地工作。 Python import 可以从任何类似文件的对象中导入内容,就像普通文件一样。但是,当像
.so
这样的东西恰好落入其中时,Python 无法向底层操作系统解释它想要加载一个没有物理名称的库。 distutils 作者想到的唯一解决方法是将其解压缩到临时目录中。当然它不是/site-packages/
因为/site-packages/
对于普通用户来说是不可写的。因此,您可以
将
PYTHON_EGG_DIR
设置为/tmp
,或授予用户
www
对/var/www/.python-eggs
的写入权限< br>(这样每次清理 /tmp 时文件就不会被解压缩)或者更好
按照@shalley303的建议解压鸡蛋
(并避免在运行时完全解压鸡蛋)。
This is a dark side-effect of using otherwise nice eggs mechanism.
Eggs are packages (a directory full of files) packed into one
.egg
file to simplify depolyment.They are stored in
/site-packages/
dir.As long as the files stored in the egg are
.py
files it works great. Python import can import things from any file-like object just like it was an ordinary file.But when something like
.so
happens to drop in there, python cannot explain to the underlying OS that it wants to load an library which doesn't have a physical name. And the only workaround distutils authors have thought of is unzipping it into a temp dir. Naturally it is not/site-packages/
since/site-packages/
is not writable for ordinary users.So you can either
set
PYTHON_EGG_DIR
to/tmp
, orgive user
www
write permission to/var/www/.python-eggs
(so that the files don't get unzipped every time /tmp is cleaned up) or better then
unzip the egg as suggested by @shalley303
(and avoid unzipping of the egg in the run-time altogether).
您还可以在安装后禁用 .egg。您需要进入 site-packages 目录,提取 .egg,然后将其移动到隐藏文件(或删除它,或其他方式)。
以下是我禁用 MySQLdb 模块 .egg 文件的示例,该文件在从 Zabbix 运行 python 脚本时导致此错误。
You can also disable the use of the .egg after it has been installed. You need to go into the site-packages directory, extract the .egg, and then move it to a hidden file (or delete it, or whatever).
Here is an example of what I did to disable the MySQLdb module .egg file which was causing this error when the python script was being run from Zabbix.
Python Egg 是包含 Python 模块和元数据的 zip 压缩包。 Egg 缓存是存储 Egg 提取内容的位置,以便其中包含的 Python 模块可用。
Python eggs are zip-compressed packages containing both Python modules and metadata. The egg cache is where the extracted contents of the egg are stored so that the Python modules contained within are usable.
菲利普·B·奥尔德姆是对的。您可以在代码中添加这些行:
Phillip B Oldham's right. You can add these lines in your code:
一个简单的修复方法是创建该目录并提供对其的
www-data
访问权限。A simple fix would be to create the directory and provide
www-data
access to it.第一次运行以下命令时,我在 Django 中遇到此错误。
我让它像这样工作:
I got this error in Django when running the below command the first time.
I got it to work like this:
在任何导入工作之前将其添加到我的源文件的开头
adding this at the begin of my source file before any import works