What is the Python egg cache (PYTHON_EGG_CACHE)?

五迷三道 提交于 2019-11-27 17:56:13

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

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.

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

Python eggs are zip-compressed packages containing both Python modules and metadata. The egg cache is where the the extracted contents of the egg are stored so that the Python modules contained within are usable.

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

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

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!