Fixing Python’s “Python Eggs” Error

By  on  

Let me first state this for the record: I am not a server guy. The closest I've ever gotten to compiling my own versions of code is "sudo port install ..." So when I decided to teach myself Python (creating simply database interaction, record listings, etc) and kept getting "500 Internal Server Error" messages, I thought I was doomed. I opened up the Apache error log and saw this:

[error] [client ::1] Traceback (most recent call last):
[error] [client ::1]   File "/Users/davidwalsh83/Projects/server/something.py", line 6, in <module>
[error] [client ::1]     import MySQLdb, cgi, cgitb, httplib, urllib2
[error] [client ::1]   File "build/bdist.macosx-10.6-universal/egg/MySQLdb/__init__.py", line 19, in <module>
[error] [client ::1]   File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 7, in <module>
[error] [client ::1]   File "build/bdist.macosx-10.6-universal/egg/_mysql.py", line 4, in __bootstrap__
[error] [client ::1]   File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.py", line 835, in resource_filename
[error] [client ::1]     self, resource_name
[error] [client ::1]   File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.py", line 1304, in get_resource_filename
[error] [client ::1]     self._extract_resource(manager, self._eager_to_zip(name))
[error] [client ::1]   File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.py", line 1326, in _extract_resource
[error] [client ::1]     self.egg_name, self._parts(zip_path)
[error] [client ::1]   File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.py", line 915, in get_cache_path
[error] [client ::1]     self.extraction_error()
[error] [client ::1]   File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/pkg_resources.py", line 881, in extraction_error
[error] [client ::1]     raise err
[error] [client ::1] pkg_resources.ExtractionError: Can't extract file(s) to egg cache
[error] [client ::1] 
[error] [client ::1] The following error occurred while trying to extract file(s) to the Python egg
[error] [client ::1] cache:
[error] [client ::1] 
[error] [client ::1]   [Errno 20] Not a directory: '/Library/WebServer/.python-eggs/MySQL_python-1.2.3c1-py2.6-macosx-10.6-universal.egg-tmp'
[error] [client ::1] 
[error] [client ::1] The Python egg cache directory is currently set to:
[error] [client ::1] 
[error] [client ::1]   /Library/WebServer/.python-eggs
[error] [client ::1] 
[error] [client ::1] Perhaps your account does not have write access to this directory?  You can
[error] [client ::1] change the cache directory by setting the PYTHON_EGG_CACHE environment
[error] [client ::1] variable to point to an accessible directory.
[error] [client ::1] 
[error] [client ::1] Premature end of script headers: something.py

My first thought was "Wow, that's quite a long way of telling me to 'just quit.'" Not wanting to concede defeat, I did some quick Google searching to find that the following snippet of Python code would allow me program another day:

import os
os.environ['PYTHON_EGG_CACHE'] = '/tmp'

I'd like to pretend I know what that means but I really don't know. In any event, if you start receiving those errors, this snippet could be your ticket to taking a bite out of Python.

Recent Features

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

  • By
    Interview with a Pornhub Web Developer

    Regardless of your stance on pornography, it would be impossible to deny the massive impact the adult website industry has had on pushing the web forward. From pushing the browser's video limits to pushing ads through WebSocket so ad blockers don't detect them, you have...

Incredible Demos

Discussion

  1. You have everything you need on the error:

    “The Python egg cache directory is currently set to:
    /Library/WebServer/.python-eggs
    Perhaps your account does not have write access to this directory? ”

    Python tried to access that dir but it may not exist for example. When you change the dir to /tmp it worked because you were now pointing to a directory that exists and that you can write into.

  2. @Pedro Santos: The dir was there and writable; I know I checked that.

  3. You might consider making the owner:group of that path the apache user (I think the default is “www-user”).

    That may get you around the little env tweaking. HTH
    -Eric

  4. The languages I’m trying to learn right now are JavaScript, PHP, and Python. I have to agree, a list of errors like that suck.

    Well, at least you bypassed it. Where there’s a problem, there is a solution. ;)

  5. Uri Merhav

    Thanks so much for this!

    As much as I appreciate learning something and undersrtanding what’s going on, this really is one of those things where you couldn’t care less as long as it solves the problem. Guess what? It freaking did.

    Cheers

  6. Ravi

    Hi,

    i also got pythons egg error.

    but adding below thing in my wsgi file does not solve my problem.

    import os
    os.environ[‘PYTHON_EGG_CACHE’] = ‘/tmp’

    Could you please help me in resolving the issue.

  7. Rafał Błaczkowski

    Hi all!
    I had the same error and I’ve resolved it successfully!
    This is a problem with paramiko library.
    If you have open one program/instance which use paramiko library, you can’t open another one which uses paramiko library too.
    If you’d like to deal with it, before import paramiko you have to set environment:
    perhaps:

    import time
    import os
    import datetime
    os.environ['PYTHON_EGG_CACHE'] = '/tmp'
    import paramiko
    

    This resolves yours problem!
    Cheers!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!