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
    7 Essential JavaScript Functions

    I remember the early days of JavaScript where you needed a simple function for just about everything because the browser vendors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent.  Times have changed but there are still a few functions each developer should...

  • By
    6 Things You Didn&#8217;t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    Optimize Your Links For Print Using CSS — Show The URL

    When moving around from page to page in your trusty browser, you get the benefit of hovering over links and viewing the link's target URL in the status bar. When it comes to page printouts, however, this obviously isn't an option. Most website printouts...

  • By
    MooTools Image Preloading with Progress Bar

    The idea of image preloading has been around since the dawn of the internet. When we didn't have all the fancy stuff we use now, we were forced to use ugly mouseover images to show dynamism. I don't think you were declared an official...

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!