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
    Facebook Open Graph META Tags

    It's no secret that Facebook has become a major traffic driver for all types of websites.  Nowadays even large corporations steer consumers toward their Facebook pages instead of the corporate websites directly.  And of course there are Facebook "Like" and "Recommend" widgets on every website.  One...

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

Incredible Demos

  • By
    Create a NoScript Compatible Select Form Element with an onChange Event

    I wouldn't say that I'm addicted to checking Google Analytics but I do check my statistics often. I guess hoping for a huge burst of traffic from some unknown source. Anyway, I have multiple sites set up within my account. The way to...

  • By
    MooTools Typewriter Effect Plugin

    Last week, I read an article in which the author created a typewriter effect using the jQuery JavaScript framework. I was impressed with the idea and execution of the code so I decided to port the effect to MooTools. After about an hour of coding...

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!