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
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

Incredible Demos

  • By
    Duplicate the jQuery Homepage Tooltips

    The jQuery homepage has a pretty suave tooltip-like effect as seen below: The amount of jQuery required to duplicate this effect is next to nothing;  in fact, there's more CSS than there is jQuery code!  Let's explore how we can duplicate jQuery's tooltip effect. The HTML The overall...

  • By
    Create Digg URLs Using PHP

    Digg recently came out with a sweet new feature that allows users to create Tiny Digg URLs which show a Digg banner at the top allowing easy access to vote for the article from the page. While I love visiting Digg every once in a...

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!