sqlite3.connect() not working in python 3.3 - python

It's probably something quite easy but I can't figure out why my script won't work. I'm trying to make a connection with my sqlite3 database but eclipse returns the error: "Undefined variable from import: connect". I'm running python 3.3 in a virtualenv on linux. Thanks for your help!
from urllib.request import urlopen
import datetime
import sqlite3
class Crawler():
def storeContent(self, html, url):
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("INSERT .. ", [item, item])
c.commit()
c.close()

It seems like Alex Barcelo resolved this issue here.
What worked for me on Ubuntu was almost the same*:
cd /usr/lib/python2.7/lib-dynload/
sudo ln -s _sqlite3.x86_64-linux-gnu.so _sqlite3.so
After that, I had to reconfigure the Python Interpreter for my PyDev project:
Project Properties -> PyDev-Interpreter/Grammar -> Click here to configure an interpreter not listed, then delete, run auto-config for the python environment you're using, and hit "Apply".
*Replace "python2.7" with the version of python you're using sqlite3 with, and if "_sqlite3.x86_64-linux-gnu.so" is not the right name of the file for your linux system, you can normally search for it using "locate _sqlite3"

Related

No application is registered as handling this file in bundled Python program

I am using Ubuntu 21.04 with Python 3.8, Matplotlitb 3.4.3, pyinstaller 4.5.1 and I would like python program to open google.com page in default web browser. There is also matplotlib package (the reason why it's not working, without matplotlib everything is fine).
#!/usr/bin/python3
import webbrowser
import matplotlib.pyplot as plt
plt.plot([1, 2, 4, 8, 16])
plt.show()
webbrowser.open("https://www.google.com/")
When I run this program as ./main.py or python3 main.py, everything is ok and Google Chrome is opened. However when I bundle program into executable using pyinstaller:
pyinstaller --onefile main.py
And run this bundled program as ./dist/main, following error appears in terminal:
gio https://www.google.com/: No application is registered as handling this file
When I define BROWSER variable before running file, then it work again:
export BROWSER=$(which google-chrome) && ./dist/main
However, I would like to run program without declaring that variable. Default browser doesn't have to be Chrome and even OS doesn't have to be Linux, but eg. Windows. I found question with similar problem. But when I downgrade matplotlib from 3.4.3 to 3.0.3 is in the link, there is a lot of compatibility errors. So I would like to keep newest matplotlib.
Is there any way fix the gio error?
Seems like the problem is caused by various environment variables being messed up. More info here: https://github.com/pyinstaller/pyinstaller/issues/3668
As a workaround you might be able to use subprocess to open the https URL with its default handler, which should be the default browser:
import os
import platform
import subprocess
env = dict(os.environ)
lp_key = 'LD_LIBRARY_PATH'
lp_orig = env.get(lp_key + '_ORIG')
if lp_orig is not None:
env[lp_key] = lp_orig
else:
lp = env.get(lp_key)
if lp is not None:
env.pop(lp_key)
opener = {'Darwin': 'open', 'Windows': 'start'}.get(platform.system(), 'xdg-open')
subprocess.run((opener, 'https://www.google.com/'), env=env)

No module named _sqlite3 using python 2.7.13 from streamlabs chatbot

The problem
I am trying to add the sqlite module to my python script that uses 2.7.13 in the streamlabs chatbot app.
When I run my script from 2.7.13 idle it works no problem. However when I run it from the streamlabs chatbot it gives the error no module named _sqlite3.
The streamlabs chatbot allows you to select the directory of where your lib is which mine is set to. I checked the lib folder and it has an sqlite folder inside of it already.
https://streamlabs.com/content-hub/post/chatbot-scripts-desktop
I assume that the module would need to be placed where the other scripts are which is C:\users\user folder\app data\roaming\streamlabs chatbot\services\scripts. For example, the boiler plate example made on git hub here:https://github.com/AnkhHeart/Streamlabs-Chatbot-Python-Boilerplate/tree/master/Boilerplate shows a lib folder in the specific script folder where he can import his custom module. Would have to do the same thing to make sqlite work in the streamlabs chatbot?
Here is the code:
import sys
import os
import time
import sqlite
ScriptName = "VLC Search and play"
Website = "twitch.tv/masterjx9"
Description = "VLC search and play app"
Creator = "masterjx9"
Version = "1.0.0"
settings = {}
searchRequest = ""
resetTime = 0
#more stuff
Here is the result in idle python:
>>>
#which is perfect
Here is the result in streamlabs chatbot:
no module named _sqlite3
Any ideas or suggestions would be awesome. Thank you
I was able to find my own answer thanks to the Streamlabs chatbot discord channel.
Streamlabs uses ironpython instead of python and they handle somethings different. They were able to give me an example of a script that uses a sqlite db and the major difference is that you need the following in order for sqlite3 to be imported:
import clr
clr.AddReference("IronPython.SQLite.dll")
clr.AddReference("IronPython.Modules.dll")
#Now you can import SQLite as shown in the line below
import sqlite3
#no errors in the chat bot
Hope this helps for anyone else.

Twittersearch module is not getting imported to api file

I am trying to make twitter search api everything was working fine but suddenly twittersearch() module is not getting imported. I am using python 3.4.2 with windows 8.1 64-bit. I have tried easy_install twittersearch it successfully installs packages and everything is fine but when I run this code
from TwitterSearch import *
import pyodbc
cnxn = pyodbc.connect(driver='{SQL Server}', server='localhost', database='capstone',trusted_connection='yes')
cursor = cnxn.cursor()
cursor.execute("select word from dbo.search where sl in (select max(sl) from dbo.search)")
for row in cursor.fetchall():
print (row)
print("This is positive data ")
term = row[0]
try:
tso=TwitterSearchOrder()
tso.set_keywords([term])
tso.set_language('en')
When i execute this it shows errors like below
Traceback (most recent call last):
File "C:\Python34\search_test.py", line 18, in <module>
tso=TwitterSearchOrder()
NameError: name 'TwitterSearchOrder' is not defined
But it is actually there I don't know why it is not recognizing the module. till 2 days back it was running successfully with IDLE but not in commandprompt and I have reinstalled the python and added all tools, now it is showing this error in both IDLE and command prompt
TIA
Make sure that TwitterSearch is in your lib\site-packages path. If it is you must've installed it right. If not, a simple python -m pip install TwitterSearch from command prompt will do.
However, there seems to be an issue(bug) with TwitterSearch wrt its functions. You might consider using an alternate API in the meantime. I would suggest using Tweepy

Using portable python to connect to an Access database

I am wanting to use portable python 2.7.x to connect to an Access database. I can't seem to get it working as it doesn't have the pyodbc libraries. Is there another way to use portable python to connect?
The newest version of portable python has an option to install pyodbc but you have to select the option it doesn't go in by default.
Click on the modules option
Select the option for pyodbc
I have did it in different way.. .
follow what i have just done on my mac snow leopard!!
Download the the pyodbc's source from where it is on internet.
Extract and 'cd' into that dir.. . Run 'python setup.py build' and then take 'pyodbc.so' file from that build's dir. Make new python file named as 'pyodbc.py' and write the content given below.(and put that 'pyodbc.so' file with it)
def __bootstrap__():
global __bootstrap__, __loader__, __file__
import sys, pkg_resources, imp
__file__ = pkg_resources.resource_filename(__name__,'pyodbc.so')
__loader__ = None; del __bootstrap__, __loader__
imp.load_dynamic(__name__,__file__)
__bootstrap__()
(remember put above code in file named as 'pyodbc.py' and put that 'pyodbc.so' file with that)
and at last ..put all these where ever you want to use or in run time add that location into sys.path as:
>>> import sys
>>> sys.path.insert(0,"/my_portable/location") # location to dir which contains those two files
after doing all this i have put those two files with my test python file..and in that i am able to import 'pyodbc' without installing it.
>>> import pyodbc
>>> dir(pyodbc)

How can I get sqlite working on a shared hosting server?

I'm trying to run a python script using python 2.6.4. The hosting company has 2.4 installed so I compiled my own 2.6.4 on a similar server and then moved the files over into ~/opt/python. that part seems to be working fine.
anyhow, when I run the script below, I am getting ImportError: No module named _sqlite3 and I'm not sure what to do to fix this.
Most online threads mention that sqlite / sqlite3 is included in python 2.6 - so I'm not sure why this isn't working.
-jailshell-3.2$ ./pyDropboxValues.py
Traceback (most recent call last):
File "./pyDropboxValues.py", line 21, in
import sqlite3
File "/home/myAccount/opt/lib/python2.6/sqlite3/__init__.py", line 24, in
from dbapi2 import *
File "/home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.py", line 27, in
from _sqlite3 import *
ImportError: No module named _sqlite3
I think I have everything set up right as far as the directory structure.
-jailshell-3.2$ find `pwd` -type d
/home/myAccount/opt
/home/myAccount/opt/bin
/home/myAccount/opt/include
/home/myAccount/opt/include/python2.6
/home/myAccount/opt/lib
/home/myAccount/opt/lib/python2.6
/home/myAccount/opt/lib/python2.6/distutils
/home/myAccount/opt/lib/python2.6/distutils/command
/home/myAccount/opt/lib/python2.6/distutils/tests
/home/myAccount/opt/lib/python2.6/compiler
/home/myAccount/opt/lib/python2.6/test
/home/myAccount/opt/lib/python2.6/test/decimaltestdata
/home/myAccount/opt/lib/python2.6/config
/home/myAccount/opt/lib/python2.6/json
/home/myAccount/opt/lib/python2.6/json/tests
/home/myAccount/opt/lib/python2.6/email
/home/myAccount/opt/lib/python2.6/email/test
/home/myAccount/opt/lib/python2.6/email/test/data
/home/myAccount/opt/lib/python2.6/email/mime
/home/myAccount/opt/lib/python2.6/lib2to3
/home/myAccount/opt/lib/python2.6/lib2to3/pgen2
/home/myAccount/opt/lib/python2.6/lib2to3/fixes
/home/myAccount/opt/lib/python2.6/lib2to3/tests
/home/myAccount/opt/lib/python2.6/xml
/home/myAccount/opt/lib/python2.6/xml/parsers
/home/myAccount/opt/lib/python2.6/xml/sax
/home/myAccount/opt/lib/python2.6/xml/etree
/home/myAccount/opt/lib/python2.6/xml/dom
/home/myAccount/opt/lib/python2.6/site-packages
/home/myAccount/opt/lib/python2.6/logging
/home/myAccount/opt/lib/python2.6/lib-dynload
/home/myAccount/opt/lib/python2.6/sqlite3
/home/myAccount/opt/lib/python2.6/sqlite3/test
/home/myAccount/opt/lib/python2.6/encodings
/home/myAccount/opt/lib/python2.6/wsgiref
/home/myAccount/opt/lib/python2.6/multiprocessing
/home/myAccount/opt/lib/python2.6/multiprocessing/dummy
/home/myAccount/opt/lib/python2.6/curses
/home/myAccount/opt/lib/python2.6/bsddb
/home/myAccount/opt/lib/python2.6/bsddb/test
/home/myAccount/opt/lib/python2.6/idlelib
/home/myAccount/opt/lib/python2.6/idlelib/Icons
/home/myAccount/opt/lib/python2.6/tmp
/home/myAccount/opt/lib/python2.6/lib-old
/home/myAccount/opt/lib/python2.6/lib-tk
/home/myAccount/opt/lib/python2.6/hotshot
/home/myAccount/opt/lib/python2.6/plat-linux2
/home/myAccount/opt/lib/python2.6/ctypes
/home/myAccount/opt/lib/python2.6/ctypes/test
/home/myAccount/opt/lib/python2.6/ctypes/macholib
/home/myAccount/opt/share
/home/myAccount/opt/share/man
/home/myAccount/opt/share/man/man1
And finally the contents of the sqlite3 directory:
-jailshell-3.2$ find `pwd`
/home/myAccount/opt/lib/python2.6/sqlite3
/home/myAccount/opt/lib/python2.6/sqlite3/__init__.pyo
/home/myAccount/opt/lib/python2.6/sqlite3/dump.pyc
/home/myAccount/opt/lib/python2.6/sqlite3/__init__.pyc
/home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.pyo
/home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.pyc
/home/myAccount/opt/lib/python2.6/sqlite3/dbapi2.py
/home/myAccount/opt/lib/python2.6/sqlite3/dump.pyo
/home/myAccount/opt/lib/python2.6/sqlite3/__init__.py
/home/myAccount/opt/lib/python2.6/sqlite3/dump.py
I feel like I need to add something into the sqlite3 directory - maybe sqlite3.so? But I don't know where to get that.
What am I doing wrong here? Please remember that I'm using a shared host so that means installing / compiling on another server and then copying the files over. Thanks! :)
Update
Just wanted to confirm that the answer from #samplebias did work out very well. I needed to have the dev package installed on the machine I was compiling from to get it to add in sqlite3.so and related files. Also found the link in the answer very helpful. Thanks #samplebias !
Python's build system uses a setup.py file to compile all of the native extensions, including sqlite3. It searches common operating system paths for the sqlite3 include and library dirs. If the sqlite3 development package is not installed Python will skip compiling the _sqlite3.so extension, but the pure Python portion of the sqlite3 package will still be installed.
You would need to have the operating system's sqlite3 development package installed when you compile Python and at runtime: sqlite3-devel on Centos, both libsqlite3-0 and libsqlite3-dev on Ubuntu.
Here's an example of the _sqlite3.so extension linkage on my Ubuntu system:
% ldd /usr/lib/python2.6/lib-dynload/_sqlite3.so | grep sqlite3
libsqlite3.so.0 => /usr/lib/libsqlite3.so.0 (0x00007f29ef3be000)
% dpkg -S /usr/lib/libsqlite3.so.0
libsqlite3-0: /usr/lib/libsqlite3.so.0
In general, the first thing to do is to ask your host. I seems a bit odd that SQLite is not installed (or installed properly). So they'll likely fix it quite fast if you ask them.
For python 2.4, you need sqlite and bindings, pysqlite 2 or aspw
None of the files listed in the sqlite folder is the _sqlite3.pyd Python shared library. Are you sure you compiled it when compiling Python? What does the build log say? I think there's a configure flag that needs to be passed.
Alternatively, just install pysqlite

Categories