Cannot import SQLite with Python 2.6 - python

I'm running Python 2.6 on Unix and when I run the interactive prompt (SQLite is supposed to be preinstalled) I get:
[root#idev htdocs]# python
Python 2.6 (r26:66714, Oct 23 2008, 16:25:34)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sqlite
>>>
How do I resolve this?

The error:
ImportError: No module named _sqlite3
means that SQLite 3 does not find the associated shared library. On Mac OS X it's _sqlite3.so and it should be the same on other Unix systems.
To resolve the error you have to locate the _sqlite3.so library on your computer and then check your PYTHONPATH for this directory location.
To print the Python search path enter the following in the Python shell:
import sys
print sys.path
If the directory containing your library is missing you can try adding it interactively with
sys.path.append('/your/dir/here')
and try
import sqlite3
again. If this works you have to add this directory permanently to your PYTHONPATH environment variable.
PS: If the library is missing you should (re-)install the module.

import sqlite3
sqlite3 - DB-API 2.0 interface for SQLite databases.
You are missing the .so (shared object) - probably an installation step. In my Linux python installation, _sqlite3 is at:
${somewhere}/lib/python2.6/lib-dynload/_sqlite3.so

Python 2.6 detects where the sqlite3 development headers are installed, and will silently skip building _sqlite3 if it is not available. If you are building from source, install sqlite3 including development headers. In my case, sudo yum install sqlite-devel sorted this out on a CentOS 4.7. Then, rebuild Python from source code.

Try this:
from pysqlite2 import dbapi2 as sqlite

On my system _sqlite3.so located at:
'/usr/lib/python2.6/lib-dynload/_sqlite3.so'
Check that the directory is in your sys.path:
>>> import sys; print(filter(lambda p: 'lib-dynload' in p, sys.path))
['/usr/lib/python2.6/lib-dynload']

Does that fix your problem?
Python 2.5.4 (r254:67916, May 31 2009, 16:56:01)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named sqlite
>>> import sqlite3
>>>

The 2.5.5. Mac port of Python 2.5 now has this hint:
"py25-sqlite3 #2.5.4 (python, databases)
This is a stub. sqlite3 is now built with python25"
And so an upgrade of the python25 port to python25 #2.5.5_0 made the import work again.
Since sqlite3 is among the dependencies of python25,
it is built anew when upgrading python25.
Thus,
$ sudo port upgrade python25
does the trick on Mac OS X, ports collection.

I face the same problem.
Steps to solve.
Download latest sqlite3 from sqlite website. sqlite-autoconf*
Install that in 3 simple steps
./configure
make
make install
Rebuild the python
make
make install

Related

Python 3.11 installation: some modules are missing

I'll try to be as clear and specific as possible because I've seen so many people with the same problem, but none of the answers they had worked for me.
The problem in question is: missing modules when trying to import other modules, that (by far as I understand) are installed by default in Python 3.11. eg: missing _sqlite3 when importing sqlite3.
Python 3.11.0 (main, Jan 12 2023, 03:19:52) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user_name/opt/Python-3.11.0/lib/python3.11/sqlite3/__init__.py",
line 57, in <module> from sqlite3.dbapi2 import *
File "/home/user_name/opt/Python-3.11.0/lib/python3.11/sqlite3/dbapi2.py",
line 27, in <module> from _sqlite3 import *
ModuleNotFoundError: No module named '_sqlite3'
Context
I'm currently working on a Dreamhost VPS, running on Ubuntu 20.04.5. Since it's a Dreamhost VPS keep in mind I can't uso sudo.
I've Python 3.8 installed globally, but I'm trying to set Python 3.11 with venv. The steps to install Python 3.11 were, as indicated by 'Installing a custom version of Python 3 by Dreamhost':
cd /home/user_name/opt
wget https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tgz
tar zxvf Python-3.11.0.tgz
cd Python-3.11.0
./configure --prefix=$HOME/opt/Python-3.11.0
make
make install
When running the make command I saw the following message:
The necessary bits to build these optional modules were not found:
_bz2 _curses _curses_panel
_lzma _tkinter _uuid
readline
To find the necessary bits, look in setup.py in detect_modules() for the module's name.
The following modules found by detect_modules() in setup.py have not
been built, they are *disabled* by configure:
_sqlite3
This is my detect_modules() in setup.py:
def detect_modules(self):
# remove dummy extension
self.extensions = []
# Some C extensions are built by entries in Modules/Setup.bootstrap.
# These are extensions are required to bootstrap the interpreter or
# build process.
self.detect_simple_extensions()
self.detect_test_extensions()
self.detect_readline_curses()
self.detect_crypt()
self.detect_openssl_hashlib()
self.detect_hash_builtins()
self.detect_dbm_gdbm()
self.detect_sqlite()
self.detect_platform_specific_exts()
self.detect_nis()
self.detect_compress_exts()
self.detect_expat_elementtree()
self.detect_multibytecodecs()
self.detect_decimal()
self.detect_ctypes()
self.detect_multiprocessing()
self.detect_tkinter()
self.detect_uuid()
# Uncomment the next line if you want to play with xxmodule.c
# self.add(Extension('xx', ['xxmodule.c']))
self.addext(Extension('xxlimited', ['xxlimited.c']))
self.addext(Extension('xxlimited_35', ['xxlimited_35.c']))
So after running the commands I just mentioned, I proceeded to setup the virtualenv to test Python 3.11:
cd /home/user_name/opt
/home/user_name/opt/Python-3.11.0/bin/python3 -m venv venv
source venv/bin/activate
And then I got the ModuleNotFoundError I mentioned at the start of the question.
Things I tried/saw:
When I checked my Python 3.11 directory, I saw that in /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload the cpython files for the modules I don't have are just not there, e.g: /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-311-x86_64-linux-gnu.so* where as in my Python 3.8, they're indeed there: /usr/lib/python3.8/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so
I tried copying the cpython file from my 3.8 to my 3.11 and renaming it:
cp /usr/lib/python3.8/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload
mv /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-38-x86_64-linux-gnu.so /home/user_name/opt/Python-3.11.0/lib/python3.11/lib-dynload/_sqlite3.cpython-311-x86_64-linux-gnu.so
Then when I ran Python 3.11 I got the following error:
Python 3.11.0 (main, Jan 12 2023, 04:33:33) [GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import _sqlite3 # this works just fine
>>> import sqlite3 # throws error
356306 segmentation fault (core dumped) python
Tried recompiling Python and running ./configure --prefix=$HOME/opt/Python-3.11.0 --enable-loadable-sqlite-extensions and didn't work.
Hope I was clear enough and thanks in advance.

No module named 'mysql'

I've created a Python script to store data in MySQL. It works properly in PyCharm. When converted to an exe file it does not work.
This is traceback from the command line:
Traceback (most recent call last):
File "myscript.py", line 1, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
[8428] Failed to execute script 'myscript' due to unhandled exception!
The command to create the exe:
pyinstaller --onefile --console CoinVolumes.py
I also tried auto-py-to-exe but got the same error. Mysql is installed.
use this command:
pip install mysql-connector-python
after that verify:
$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>
In Python 3.6.9 I can use Pip 3 and do following:
pip3 install mysql
pip3 install mysql-connector-python
You would need to run the mysql server yourself. Pyinstaller is working properly, the problem here is that mysql server is not part of python, therefore you need to start the server mannualy. An alternative is to use sqlite, which does not require a server. It is built into standard python, no need for installing anything.

ImportError importing ssl in MinGW-W64 Python 3.6

I've used MSYS2 to install the MinGW-W64 version of Python 3.6 (mingw-w64-x86_64-python3). Upon importing ssl, an ImportError is raised:
$ python3
Python 3.6.2 (default, Aug 15 2017, 10:59:43) [GCC 7.1.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "G:/msys64/mingw64/lib/python3.6\ssl.py", line 101, in <module>
import _ssl # if we can't import it, let the error propagate
ImportError: DLL load failed: The specified procedure could not be found.
>>>
I'm not sure why this. pacman shows both mingw-w64-x86_64-openssl and openssl as installed. /mingw64/lib/python3.6/lib-dynload/_ssl-cpython-36m.dll exists. Other .dll files from the same directory (like those used for the json and sqlite3 modules) import fine.
To make matters weirder, the Python 2 version (mingw-w64-x86_64-python2) doesn't have this problem.
This is not a correct solution (the MSYS2 folks are working on the real solution), but as a temporary workaround for anyone else who gets stuck on this:
After installing mingw-w64-x86_64-python3 copy
C:\msys64\mingw64\bin\LIBEAY32.dll and
C:\msys64\mingw64\bin\SSLEAY32.dll
to
C:\msys64\mingw64\lib\python3.6\lib-dynload\LIBEAY32.dll and
C:\msys64\mingw64\lib\python3.6\lib-dynload\SSLEAY32.dll respectively.
Afterwards import ssl (and tools that rely on it, like pip3) should work correctly.

ImportError: No module named 'paramiko'

I have done through the other questions online here, and I feel that mine is different enough to warrant a new question.
So I have a Centos 6 box, which is running a small website for me, acts as an office git server and I am trying to configure Python3 on it.
So I followed the following these steps to set up python3 on the server. However it seems that I cannot import paramiko into my script.
I downloaded the paramiko rpm however I get this message:
When I try to import paramiko I get:
[root#GIT Python-3.4.2]# rpm -ivh /usr/lib/Python-3.4.2/Modules/python-paramiko-1.7.5-2.1.el6.noarch.rpm
Preparing... ########################################### [100%]
package python-paramiko-1.7.5-2.1.el6.noarch is already installed
When I run python3 directly:
[root#GIT inserv_health_check]# python3
Python 3.4.2 (default, Jan 21 2015, 06:28:04)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named 'paramiko'
>>>
I am sure there is a simple solution to this problem perhaps the path is wrong, or I should have put a symbolic link in somewhere. Any help would be appreciated :)
Before anyone asks, which python output:
[root#GIT Python-3.4.2]# which python
/usr/bin/python
[root#GIT Python-3.4.2]# which pytho~n3
/usr/bin/which: no pytho~n3 in (/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin)
[root#GIT Python-3.4.2]# which python3
/usr/local/bin/python3
Thanks
You need to do pip install paramiko so that python sees that module. If you work on a virtual environment, you need to workon <env_name> first and then pip install the desired module.
type pip3 install paramiko
if you want to install it for python3

How to Fix the Broken BSDDB Install in the Default Python Package on Mac OS X 10.5 Leopard?

Do the following on the default Python install on Mac OS X 10.5 (Leopard) w/ Developer Tools:
noel ~ : python
Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import bsddb
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/bsddb/__init__.py", line 51, in <module>
import _bsddb
ImportError: No module named _bsddb
nice, huh? How do I fix this without giving up and installing/configuring/maintaining my own Python package as per TMNC's suggestion or using MacPorts etc?
Edit
I've gone around the problem by installing Python2.4 and BSDDB via MacPorts.
My question still stands: why is the default install broken and is it possible to fix it.
Follow the instructions at http://marc-abramowitz.com/archives/2007/11/28/hacking-os-xs-python-dbhash-and-bsddb-modules-to-work/ .
The patch did not work for me and I had to replace the bsddb folder in
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
with the bsddb3 in:
/usr/local/lib/python2.7/site-packages/bsddb3
Make sure you backup the bsddb folder just in case.
This pain persists on OSX 10.8. I could not install bsddb3 using macports py-bsddb3 into a virtualenv. What was very simple and did work is:
install db53 from macports
download and unpack bsddb3 source (https://pypi.python.org/pypi/bsddb3/6.1.0)
sudo python setup.py –berkeley-db-incdir=/opt/local/include/db53 –berkeley-db-libdir=/opt/local/lib/db53 install
A solution I found was to install ActivePython, dig into its library (located in /Library/Frameworks/Python.framework/Versions/....your version here..../lib) and copy and paste the _bsddb.so file into my OS X 10.6 native python 2.6 install directory (/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/).
That fixed the missing _bsddb issue leaving me with a working version of bsddb in my native python install. Then, I just uninstalled ActivePython (instructions here)
The error I had was "No module named _bsddb". It turn out I didn't need to upgrade my bsddb. I wasusing the .pkl file created in windows. After renaming the pkl file to get it out the way Mac OSX Python recreated a new .pkl.db file and now it works perfectly.

Categories