什么是Python Egg 缓存(PYTHON_EGG_CACHE)?

发布于 2024-08-20 11:35:48 字数 669 浏览 5 评论 0原文

我刚刚在我的开发机器上从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

固执像三岁 2024-08-27 11:35:48

根据我的调查,一些 Egg 被打包为 zip 文件,并以这种方式保存在 Python 的 site-packages 目录中。

这些压缩的 Eggs 需要先解压才能执行,因此会展开到 PYTHON_EGG_CACHE 目录中,默认情况下为 ~/.python-eggs (位于用户的主目录中)目录)。如果不存在,则会在尝试运行应用程序时出现问题。

有许多修复方法:

  1. 在用户的主目录中创建一个 .python-eggs 目录并使其可供用户写入。
  2. 创建一个用于解压的全局目录(例如/tmp/python-eggs)并将环境变量PYTHON_EGG_CACHE设置到该目录。
  3. 安装时使用 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:

  1. Create a .python-eggs directory in the user's home directory and make it writable for the user.
  2. Create a global directory for unzipping (eg. /tmp/python-eggs) and set the environment variable PYTHON_EGG_CACHE to this directory.
  3. Use the -Z switch when using easy_install to unzip the package when installing.
节枝 2024-08-27 11:35:48

python Egg 缓存只是 setuptools 用来存储符合 egg 规范 的安装包的目录。您可以在此处了解有关 setuptools 的更多信息

此外,正如错误消息所述,您可以通过设置 PYTHON_EGG_CACHE=/some/other/dir 在您的环境中指定不同的 Egg 缓存目录。最简单的方法是在 ~/.bash_profile 中设置它(假设您使用的是 bash),如下所示:

export PYTHON_EGG_CACHE=/some/other/dir

如果您使用的是 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:

export PYTHON_EGG_CACHE=/some/other/dir

You may need to set it in your Apache environment if you're using a Web application.

大海や 2024-08-27 11:35:48

这是使用原本不错的鸡蛋机制的黑暗副作用。

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

  • give 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).

全部不再 2024-08-27 11:35:48

您还可以在安装后禁用 .egg。您需要进入 site-packages 目录,提取 .egg,然后将其移动到隐藏文件(或删除它,或其他方式)。

以下是我禁用 MySQLdb 模块 .egg 文件的示例,该文件在从 Zabbix 运行 python 脚本时导致此错误。

cd /usr/local/lib/python2.7/site-packages
unzip MySQL_python-1.2.3-py2.7-linux-x86_64.egg
mv MySQL_python-1.2.3-py2.7-linux-x86_64.egg .MySQL_python-1.2.3-py2.7-linux-x86_64.egg

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.

cd /usr/local/lib/python2.7/site-packages
unzip MySQL_python-1.2.3-py2.7-linux-x86_64.egg
mv MySQL_python-1.2.3-py2.7-linux-x86_64.egg .MySQL_python-1.2.3-py2.7-linux-x86_64.egg
梦里泪两行 2024-08-27 11:35:48

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.

罪#恶を代价 2024-08-27 11:35:48

菲利普·B·奥尔德姆是对的。您可以在代码中添加这些行:

import os  
os.environ['PYTHON_EGG_CACHE'] = '/tmp' # a writable directory 

Phillip B Oldham's right. You can add these lines in your code:

import os  
os.environ['PYTHON_EGG_CACHE'] = '/tmp' # a writable directory 
浅紫色的梦幻 2024-08-27 11:35:48

一个简单的修复方法是创建该目录并提供对其的 www-data 访问权限。

$ mkdir /var/www/.python-eggs
$ chown www-data:www-data /var/www/.python-eggs

A simple fix would be to create the directory and provide www-data access to it.

$ mkdir /var/www/.python-eggs
$ chown www-data:www-data /var/www/.python-eggs
你穿错了嫁妆 2024-08-27 11:35:48

第一次运行以下命令时,我在 Django 中遇到此错误。

python manage.py sql myproject

我让它像这样工作:

1. In Explorer, view the folder that the error says egg cache directory is set to
2. Delete (or rename) the file mysql_python-1.2.5-py2.7-win32.egg-tmp
3. That's it. The command now works and creates a new file in there. (Haven't tested if I need to do this every time.)

I got this error in Django when running the below command the first time.

python manage.py sql myproject

I got it to work like this:

1. In Explorer, view the folder that the error says egg cache directory is set to
2. Delete (or rename) the file mysql_python-1.2.5-py2.7-win32.egg-tmp
3. That's it. The command now works and creates a new file in there. (Haven't tested if I need to do this every time.)
青芜 2024-08-27 11:35:48

在任何导入工作之前将其添加到我的源文件的开头

import os
xyz = os.path.join('~', 'Documents', '.cache')
os.environ['PYTHON_EGG_CACHE'] = os.path.expanduser(xyz)

adding this at the begin of my source file before any import works

import os
xyz = os.path.join('~', 'Documents', '.cache')
os.environ['PYTHON_EGG_CACHE'] = os.path.expanduser(xyz)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文