Bundle sqldrivers into .exe using py2exe
Question: In my first attempts, my pyQt application bundled with py2exe refused to
connect to the sqlite database although it was working in its python version.
I guessed that it was a problem of libraries not loaded into the .exe
application. I solved that problem by including the full path to the sqlite
DLL into the `setup.py` file and thus copying this DLL to the executable
folder.

Now I would like to include this DLL into the .exe file in order to "hide"
this DLL to my users. **Do you have a clue how to do that ?**

my current setup.py:

    
    
    from distutils.core import setup
    import py2exe
    
    setup(
      windows=[{
          "script": 'myscript.py'
      }],
      options={
          'py2exe': {
              "dll_excludes": [
                  "MSVCP90.dll",
                  "MSWSOCK.dll",
                  "mswsock.dll",
                  "powrprof.dll",
                  ],
    
              'includes': [
                  'sip',
                  'PyQt4.QtNetwork',
                  ],
             'bundle_files': 1,
          }
      },
      data_files = [
            'config.ini',
            'template.htm',
    
            # This is the File that I wish to be "hidden"
            ('sqldrivers', ('C:\Python27\Lib\site-packages\PyQt4\plugins\sqldrivers\qsqlite4.dll',)),
    
      zipfile=None,
    )
    


Answer: I ran into the same problem and you are half way to solving the issue. The
first part of the problem is as you identified, getting the file into the EXE.
I can't speak to the correctness of your py2exe solution as I am using
pyinstaller, but that is the general idea. You need to get the qsqlite4.dll
into a sqldrivers directory within your single file app.

The second part is that your main .py needs to have the path added to its
running directory which will now contain that sqldrivers folder. What you will
need to do is get the relative path to where your main .py is running and set
that directory as your library path in your QT application. I use the standard
resource_path() function for pyinstaller, but using something like this should
work for py2exe:

    
    
    def resource_path(relative_path)
        if sys.frozen:
            base_path = os.path.dirname(sys.executable)
        else:
            base_path = os.path.dirname(__file__)
        return os.path.join(base_path, relative_path)
    

Then you can use this code in the main function of your application

    
    
    app = QApplication(sys.argv)
    new_lib_path = app.libraryPaths()
    new_lib_path.append(resource_path(''))
    app.setLibraryPaths(new_lib_path)
     . . .
    

With logging added, here is my app.libraryPaths() before and after:

    
    
    08/25/2014 01:33:24 AM CRITICAL: Before[u'C:/dev/WORKSP~1/db/dist']
    08/25/2014 01:33:24 AM CRITICAL: After[u'C:/dev/WORKSP~1/db/dist', u'C:\\Users\\jeff\\AppData\\Local\\Temp\\_MEI2042\\']
    

You could replace the '\' with '/' but I didn't bother, it still works with
windows separators.

