--- Python/pythonrun.c
+++ Python/pythonrun.c
@@ -41,6 +41,15 @@
 				   _Py_GetRefTotal())
 #endif
 
+#include <unistd.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#define IS_EXEC_FILE(mode) \
+    (((mode) & S_IFREG) && ((mode) & (S_IXUSR | S_IXGRP | S_IXOTH)))
+
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -1120,10 +1129,88 @@
 	Py_XDECREF(tb);
 }
 
+/*
+ * def get_python_lib():
+ *   import distutils.sysconfig
+ *   return distutils.sysconfig.get_python_lib()
+ */
+char* chr_GetPythonLib() {
+    PyObject *pSysconfig, *pGetPythonLib;
+    PyObject *pPythonLib;
+    char* ret = NULL;
+
+    pSysconfig = PyImport_ImportModule("distutils.sysconfig");
+
+    if (pSysconfig != NULL) {
+        pGetPythonLib = PyObject_GetAttrString(pSysconfig, "get_python_lib");
+
+        if (pGetPythonLib && PyCallable_Check(pGetPythonLib)) {
+
+            pPythonLib = PyObject_CallObject(pGetPythonLib, NULL);
+
+            if (pPythonLib != NULL) {
+                ret = PyString_AsString(pPythonLib);
+                Py_DECREF(pPythonLib);
+            }
+            else {
+                //TODO: if (!PyErr_ExceptionMatches(PyExc_BaseException))
+                Py_DECREF(pGetPythonLib);
+                Py_DECREF(pSysconfig);
+                PyErr_Print();
+                return NULL;
+            }
+        } else {    // if (pGetPythonLib && PyCallable_Check(pGetPythonLib))
+            if (PyErr_Occurred())
+                PyErr_Print();
+        }
+        Py_XDECREF(pGetPythonLib);
+        Py_XDECREF(pSysconfig);
+    } else {        // if (pSysconfig != NULL)
+        PyErr_Print();
+        return NULL;
+    }
+    return ret;
+}
+
+void CallImportErrorHandler(PyObject *s) {
+    pid_t pid;
+    int status, died;
+    const char* handler = "python_import_error_handler";
+    const char* handler_path = chr_GetPythonLib();
+    int path_len;
+    const char* path;
+    struct stat handler_stat;
+
+    if ((handler_path == NULL) && (! isatty(STDOUT_FILENO))) {
+        return;
+    }
+
+    path = malloc(strlen(handler)+strlen(handler_path)+2);
+    sprintf(path, "%s/%s", handler_path, handler);
+
+    if (! stat(path, &handler_stat)) {
+        if (IS_EXEC_FILE(handler_stat.st_mode)) {
+
+            switch(pid=fork()){
+                //case -1: this should not happens
+                case 0:
+                    execl (path, handler, (char*)((PyStringObject*)s)->ob_sval, (char *)0);
+                    exit(3); 
+                default: died= wait(&status);
+            }
+
+        }
+    }
+
+    free(path);
+    path = NULL;
+}
+
 void
 PyErr_Display(PyObject *exception, PyObject *value, PyObject *tb)
 {
 	int err = 0;
+        int import_err = 0;
 	PyObject *f = PySys_GetObject("stderr");
 	Py_INCREF(value);
 	if (f == NULL)
@@ -1191,8 +1278,10 @@
 			if (err == 0) {
 				if (className == NULL)
 				      err = PyFile_WriteString("<unknown>", f);
-				else
+				else {
 				      err = PyFile_WriteString(className, f);
+                                      import_err = ! strcmp(className, "ImportError");
+                                }
 			}
 		}
 		else
@@ -1207,8 +1296,12 @@
 			else if (!PyString_Check(s) ||
 				 PyString_GET_SIZE(s) != 0)
 				err = PyFile_WriteString(": ", f);
-			if (err == 0)
+			if (err == 0) {
 			  err = PyFile_WriteObject(s, f, Py_PRINT_RAW);
+                          if (import_err) {
+                              CallImportErrorHandler(s);
+                          }
+                        }
 			Py_XDECREF(s);
 		}
 		if (err == 0)
@@ -1221,6 +1314,7 @@
 		PyErr_Clear();
 }
 
+
 PyObject *
 PyRun_StringFlags(const char *str, int start, PyObject *globals,
 		  PyObject *locals, PyCompilerFlags *flags)
