Error while dumping out data from sqlite3 - python

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)

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.

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?

Related

Python Firebase (Real-time database) on Replit

I want to connect to firebase on replit using python. I'm using the code below:
from firebase import firebase
firebase = firebase.FirebaseApplication("my firebase url", None)
data = {
'ddfjaklfbja': 'dklabja;kdlf',
'adfjlkgadjf': 'dkaljbaowq',
'afbiouadofpia': 'dsajklbjanzm'
}
result = firebase.post('/test1', data)
print(result)
However, when I try to run it, it returns an error saying that there is no module named urllib3:
ModuleNotFoundError: No module named 'urllib3'
When I try to use the packages tab to install it on the left, it does not install and returns with the same error (there are multiple lines but the last line says this). How can I fix it?
Their are multiple things you can try:
Open your command prompt and enter "pip install urllib3"
If it can't install it try this: python3 -m pip install urllib3 or pip3 install urllib3
Perhaps it helps when you add this to your python file: import urllib3
this issue is related to Python path, and with the python interpreter. Make sure you are using the python interpreter in which you have installed the modules. (If using 3.9 use python 3.9 as interpreter as opposed to 3.10). If this doesnt work check python path. Other easy solution is to change your project folder to one that imported your modules correctly.

Install a python module in the same directory as the main handler

Hello I want to install a module named python-ldap locally in the same directory as my main so that it could be zipped and uploaded as a standalone function. The reason is AWS Lambda doesn't support installing this module (but i have installed it successfully on AmazonLinux). So I'm hoping i can install the module in an AmazonLinux instance and zip it so it runs on any instance. If its possible that is.
For example purposes i have a folder deploy-ldap with a single lambda_function.py inside.
The lambda_function.py simply imports the module like so:
import ldap
def main():
print("Success")
What I tried so far:
There are some resources on this suggesting to copy a single .so file but it didn't work for me and resulted in an error where another .so.2 file is being requested
Furthermore i tried installing the module with pip install python-ldap -t . but this also resulted in an error: "Unable to import module 'lambda_function': No module named '_ldap'"
All input appreciated, thank you. ^^
The import python-ldap is incorrect because module names in Python shouldn't contain dashes. The correct import as in the examples should be:
import ldap
Then to make it available in the environment of your AWS Lambda function, you should follow the same steps as documented in Deployment package with dependencies which consists of the following steps:
Prepare the file containing the code
Perform pip install python-ldap
Deploy the code along with the installation to AWS Lambda

Add python modules to Azure

When I run a python script that I set up on WebJobs in Azure - I get the following error:
import MySQLdb
ImportError: No module named MySQLdb
job failed due to exit code 1
I found some articles that seem to suggest to install python modules to a directory created on the webapp. How/Where would I install those modules?
Have you tried this?
http://nicholasjackson.github.io/azure/python/python-packages-and-azure-webjobs/
(from the site):
Step 1.
If you are using OSX and the default Python 2.7 install your packages
installed with pip will be in /usr/local/lib/python2.7/site-packages,
create a folder called site-packages in the root of your python job
and copy any packages you need for your job into it.
Step 2
Next you need to modify your run.py or any other file which requires
access to the package files. At the top of the file add….
import sys
sys.path.append("site-packages")

ubuntu - bz2 not available (/usr/bin/python older version, so am using /home/name/somedir for python

My server has python older version(2.6~), so I have created a separate dir for installing latest python. I have installed from binary .
Now, got error that 'bz2 module is not available'
got this problem when installing django-celery.
it is actually working from system level python like this:
/usr/bin/python:
python shell opens......
then, import bz2; works !!!
python (means, locally installed python, after source /venv/bin/activate)
in python shell, import bz2; says that it is not available. Can you please make it work for my local version( not global one).
Resources I found and tried from :
ImportError: No module named bz2 for Python 2.7.2
Python's bz2 module not compiled by default
A very similar question describes the same problem but for zlib. In short, you'll need libbz2.so and its headers in some location where Python can find them. You might need to download the bzip2 source code and compile/install that in your homedir as well.

How to get _sqlite3.so file?

I have installed Python 2.6.2.. I did it "locally" since I do not have root permissions. With this version of Python I wanted to use module called "sqlite3" (it is called "pysqlite" in earlier versions). In theory I had to be able to use this module without any problems since it is supposed to be included by default in the considered version of Python. However, I have some troubles. When I type:
from sqlite3 import *
I get:
Traceback (most recent call last):
File "", line 1, in File "/home/verrtex/opt/lib/python2.6/sqlite3/init.py", line 24, in
from dbapi2 import *
File "/home/verrtex/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in
from _sqlite3 import *
ImportError: No module named _sqlite3
As I have already told to, the possible reason of this problem is that the module in tries to import _sqlite3 and fails, so it's not finding _sqlite3.so. This explanations is supported by the fact that I do not have _sqlite3.so file in my "/home/verrtex/opt/lib/python2.6/lib-dynload" directory. So, this is the problem I have to solve (I have to get this file to this directory).
I found out that to solve this problem I have to "install sqlite3 and recompile Python". I also found out that the problem can be solved by "building from source and moving the library to /usr/lib/python2.5/lib-dynload/".
However, it is not clear to me what exactly should I do. Should I install python module called "sqlite3" or I should install sqlite-database? By the way, I have already sqlite-database installed globally by the administrator. Can I use it or I still have to install my own database? By the way, I do not have root permissions. Can it be a problem? Or I need to install a python module? Is absence of root permissions a problem, in this case?
I also has been told to, to take source files from SQLite Download Page, extract archive, move to expanded directory and execute:
./configure
make
make install
Then I have to copy newly compiled files to my Python directory. Should I copy all newly compiled files? And to which exactly directory should I copy (my Python directory have some subdirectories)?
Would very appreciate any help, because I stack with this problem for a wile.
P.S. My OS is CentOS release 5.3 (Final).
Your sys.path is likely not pointing to your locally installed copy, or you're not running the Python 2.6.2 you think you are.
If none of that is the case, you need the SQLite development headers (sqlite-dev or whatever), and then recompile Python. You need to pay attention at the end of the compile, because it complains about what it didn't build due to missing dependencies.
EDIT: Reread question.
EDIT 2: Also, please don't do this:
from module import *
Do this:
from module import what_i_need
import module2
Although you might have found your solution, I just wrote mine down for someone who are stuck in the same problem.
My OS is CentOS 6.3(Final) with python2.6.
I install python2.7.3 in my system, but the problem's still there. (_sqlite3.so should be in /path/to/python2.7.3/lib/python2.7/lib-dynload after python2.7.3 has been installed. Because before python2.7 was installed, sqlite-autoconf-3071502.tar.gz was installed.)
I then copy the /path/to/python2.6/lib/python2.6/lib-dynload/_sqlite3.so to the python2.7's path. And type in the python-shell:
>>> import sqlite3
or
>>> import _sqlite3
No error reports.
Unfortunately, the damn error appeared as before when I run my python script.
I install sqlite-devel(sudo yum install sqlite-devel or download here), and then reinstall python2.7.3 again. Run my python script again. Thank goodness! The damn error finally solved.

Categories