Import hashlib fails, embedded

Jeremy Cowles jeremy.cowles at
Sun Jul 19 09:12:47 CEST 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

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

Traceback (most recent call last):
  File "", line 2, in <module>
    import hashlib
  File "~/workspace/pyboinc/lib/", line 133, in
  File "~/workspace/pyboinc/lib/", line 60, in
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;


    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_InitModule("boinc", BoincMethods);

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

    PyObject* mod = PyImport_Import(name);

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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list