1

I have used sqlite3_connection.iterdump() method to dump the sqlite3 the database.

I have written a module in python that dumps out the sqlite3 tables. The module works fine if I run it locally in my machine.

And, After creating a python package of the module using pyinstaller, if I try to dump out the database it gives an error saying

"ImportError: No module named sqlite3.dump"

Any idea how I can solve this issue. Or is there any alternative to get the sqlite3 dump.

Here is what I'm following to dump the database.

#Export database
def export_database(self):
    database_string = ""
    for line in self.conn.iterdump():
            database_string += '%s\n' % (line)
    return database_string

#Import database
def import_database(self, database_string):
    self.cursor.executescript(database_string)
user2109788
  • 1,266
  • 2
  • 12
  • 29
  • So, without using your package and Pyinstaller, everything works? I'm trying to separate the packing problem from any other underlying problem with the module sqlite3. – zom-pro Aug 29 '15 at 12:28
  • Yes it works! Only after the creation of package the above problem occurs. – user2109788 Aug 29 '15 at 12:29
  • Try creating a virtualenv, then once you activate the virtualenv, try the command giving the problem and see if the module is there or not. It's difficult to replicate your problem as I've different libraries installed. – zom-pro Aug 29 '15 at 12:32
  • This is the first time I used virtual env. I tried the following. I don't know if you meant the same. Let me know if I'm wrong. I created a virtual env venv. And ran my module(without creating the package) as venv/bin/python mymodule.py after activating it(venv/bin/activate) and everything worked fine. – user2109788 Aug 29 '15 at 12:41
  • ok, so the module is working. What type of package are you building, a tarball, a wheel an exe? – zom-pro Aug 29 '15 at 13:23
  • 1
    Which version of _PyInstaller_ are you using? Do you have the file `hooks/hook-sqlite3.py` under your _PyInstaller_ installation directory? – Yoel Aug 29 '15 at 17:42
  • Yes, I'm trying to build an executable. And, hooks/hook-sqlite3.py is not present under PyInstaller. Is it something I should add? If yes, then can you please tell me how? I'm using pyinstaller2.0 – user2109788 Aug 31 '15 at 06:27
  • @Yoel I tried using PyInstaller2.1 and it worked fine. Thanks for the help! If you add it as an answer I will accept it.:) – user2109788 Aug 31 '15 at 07:01
  • Great! I've added it as an answer. Thanks :-) – Yoel Sep 01 '15 at 12:16

2 Answers2

2

Please verify that you have have the file hooks/hook-sqlite3.py under your PyInstaller installation directory. If you don't have it please install the latest PyInstaller version.


If you're unable to install the latest version, create the file hook-sqlite3.py with the following content:

from PyInstaller.hooks.hookutils import collect_submodules
hiddenimports = collect_submodules('sqlite3')

When building, supply the path to the directory in which you placed the hook file as a value to the --additional-hooks-dir argument, as follows:

--additional-hooks-dir=<path_to_directory_of_hook_file>

As per the comment below, it seems that adding --hidden-import=sqlite3 while building works as well.

Community
  • 1
  • 1
Yoel
  • 9,144
  • 7
  • 42
  • 57
  • 1
    Adding --hidden-import=sqlite3 while building also works! – user2109788 Sep 02 '15 at 06:23
  • For pyinstaller 3 you must use the following code to import **collect_submodules** : `from PyInstaller.utils.hooks import collect_submodules` [PyInstaller Documentation](https://pythonhosted.org/PyInstaller/#useful-items-in-pyinstaller-utils-hooks) – Etienne Prothon Nov 27 '15 at 07:28
0

Wherever you're deploying your module, you need to install sqlite3 module first. Most likely in your local environment, the module was available in the general python library. Try working with virtualenv to avoid this type of problems and you can use pip to install all your module's requirements.

Sqlite should be included in your Python installation but it all depends what was the source, version or how Python was installed. For more details look at here : How can I install sqlite3 to Python?

Community
  • 1
  • 1
zom-pro
  • 1,571
  • 2
  • 16
  • 32
  • Thanks for the response! But shouldn't all the required modules be bundled when I buldle the python package. Because after creating the package using pyinstaller I'm able to run the python module without installing the python library. Even creating database tables works fine. Only if I try to use the iterdump module it is saying the above mentioned error. – user2109788 Aug 29 '15 at 11:58
  • If you deploy your package in the same local machine, I would expect to use the same library that might be missing wherever you're deploying. I might be getting it wrong. Are you using virtual environments? it makes all this work so much easier. Also, which version of Python are you using? – zom-pro Aug 29 '15 at 12:00
  • I'm using python 2.7.1. And not using virtual env. Actually I also tried deploying it in the same machine. But still the error persists. Is there any alternative where i can save database state and restore it when I rerun my module/application – user2109788 Aug 29 '15 at 12:16
  • My feeling is that the problem is not in that functionality. Can you add more code to your question? – zom-pro Aug 29 '15 at 12:19
  • I have edited my question with the modules i use for exporting and importing the database. It works fine If I directly run(without creating package using pyinstaller) it in my machine. And it doesn't work after the creation of package using pyinstaller. – user2109788 Aug 29 '15 at 12:24