Import hashlib fails, embedded

Jeremy Cowles jeremy.cowles at gmail.com
Sun Jul 19 03:12:47 EDT 2009


I'm working on an embeddded Python interpreter (using the c-api) where we
are loading a custom, zipped copy of the standard Python library
(./lib/python25.zip).

Everything is working fine, but when I try to "import hashlib", i get the
following error:

Traceback (most recent call last):
  File "tryrun.py", line 2, in <module>
    import hashlib
  File "~/workspace/pyboinc/lib/python25.zip/hashlib.py", line 133, in
<module>
  File "~/workspace/pyboinc/lib/python25.zip/hashlib.py", line 60, in
__get_builtin_constructor
ImportError: No module named _md5

I assume this is related to a builtin or dynamically loaded C module, but
I'm not sure exactly what the problem is. Can anyone explain to me what is
going wrong? Other modules (like sys, os) are working fine, which may be
because they are pure-Python modules.

Here is the main function:

int main(int argc, const char* argv[])
{
    if (argc < 2) {
        cerr << "Module name missing\n";
        cerr << "usage: " << argv[0] << " module" << endl;
        return 1;
    }

    // get the python library name
    char* libname = get_lib_name();

    // try to copy the library from ./libname to ./lib/libname
    if (!copy_lib(libname)) {
        return 1;
    }

    Py_SetProgramName((char*)argv[0]);
    Py_SetPythonHome("./");
    Py_Initialize();

    if (argc > 0){
        PySys_SetArgv(argc-1, (char**)(argv+1));
    }

    {
        PyObject* syspath = PySys_GetObject("path");
        if (!syspath) {
            cerr << "Couldn't get sys.path" << endl;
            return 1;
        }
        if (!PyList_Check(syspath)) {
            cerr << "sys.path not a list" << endl;
            return 1;
        }
        PyObject* str = PyString_FromString(".");
        PyList_Append(syspath, str);
        Py_DECREF(str);
    }

    Py_InitModule("boinc", BoincMethods);

    PyObject* name = PyString_FromString(argv[1]);

    PyObject* mod = PyImport_Import(name);
    Py_DECREF(name);

    if (!mod) {
        cerr << "Error loading module" << endl;
        PyErr_Print();
        return 1;
    }
    Py_DECREF(mod);

    Py_Finalize();
}

--
Jeremy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090719/4cf2eb70/attachment.html>


More information about the Python-list mailing list