Python ImportError when attempting to import sqlite3 module
Question: I am trying to cross compile Python 2.7.3 for an arm based embedded device. I
have managed to compile it successfully (based on these instructions:
<http://randomsplat.com/id5-cross-compiling-python-for-embedded-linux.html>)
and all of the tests pass on the target device so I'm confident that the build
process works. I've cross compiled sqlite3 (version 3.8.5) and included it in
the python cross compile process which it seems to pick up fine (it is no
longer listed in the modules which were not found at the end of the build
process).

I'm having difficulty actually trying to import the sqlite3 library on the
target device, I get the error listed below (python is running with the -v
flag).

    
    
    Python 2.7.3 (default, Jul  7 2014, 19:06:12) 
    [GCC 3.4.6] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sqlite3
    
    import sqlite3 # directory /mnt/card/arm-python/lib/python2.7/sqlite3
    # /mnt/card/arm-python/lib/python2.7/sqlite3/__init__.pyc matches /mnt/card/arm-python/lib/python2.7/sqlite3/__init__.py
    import sqlite3 # precompiled from /mnt/card/arm-python/lib/python2.7/sqlite3/__init__.pyc
    # /mnt/card/arm-python/lib/python2.7/sqlite3/dbapi2.pyc matches /mnt/card/arm-python/lib/python2.7/sqlite3/dbapi2.py
    import sqlite3.dbapi2 # precompiled from /mnt/card/arm-python/lib/python2.7/sqlite3/dbapi2.pyc
    dlopen("/mnt/card/arm-python/lib/python2.7/lib-dynload/datetime.so", 2);
    import datetime # dynamically loaded from /mnt/card/arm-python/lib/python2.7/lib-dynload/datetime.so
    dlopen("/mnt/card/arm-python/lib/python2.7/lib-dynload/time.so", 2);
    import time # dynamically loaded from /mnt/card/arm-python/lib/python2.7/lib-dynload/time.so
    dlopen("/mnt/card/arm-python/lib/python2.7/lib-dynload/_sqlite3.so", 2);
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/mnt/card/arm-python/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
        from dbapi2 import *
      File "/mnt/card/arm-python/lib/python2.7/sqlite3/dbapi2.py", line 27, in <module>
        from _sqlite3 import *
    ImportError: File not found
    

It seems to be complaining about a "file not found" but I've been through all
of the paths listed in the output an all of the files seem to exist. Is there
anything I can do to diagnose this problem further?


Answer: I've managed to get it working although I don't think I fully understand
what's going on, I'm not that familiar with compiling C/C++ code and how the
whole static/shared libraries and linking works, maybe someone can shed some
light on what's actually going on here. Anyway, here's how I've resolved it.

First I ran strace (Linux debugging utility) on the python process:

    
    
    strace /mnt/card/arm-python/bin/python
    

This spits out a load of output as the python process starts, once it's
settled down I tried to import the sqlite library:

    
    
    import sqlite3
    

This will then spit out a load more output, most of it relating to opening the
files involved in the module your are importing. I then noticed it managed to
open `_sqlite3.so` but shortly afterwards it tried to load `libsqlite3.so`
which wasn't found on the library paths ($LD_LIBRARY_PATH).

    
    
    open("/mnt/card/arm-python/lib/python2.7/lib-dynload/_sqlite3.so", O_RDONLY|O_LARGEFILE) = 5
    ...
    open("/lib/libsqlite3.so.0", O_RDONLY)  = -1 ENOENT (No such file or directory)
    open("/usr/lib/libsqlite3.so.0", O_RDONLY) = -1 ENOENT (No such file or directory)
    

I copied `libsqlite3.so.0.8.6` from the /lib directory within the cross
compiled sqlite library on my build machine to `/mnt/card` on the embedded arm
device and renamed it to `libsqlite3.so.0`. I then added `/mnt/card` to the
$LD_LIBRARY_PATH as the existing locations in that path reside on the read-
only filesystem.

I then tried to import sqlite3 again and it all seem to be working fine. So
what is the role of the stuff in the `lib-dynload` and also the role of
`libsqlite3.so.0`?

