Sublime Forum

sqlite3 module in ST3

#1

Hi,

ST2 shipped with a trimmed down standard library that excluded the sqlite3 module. On Mac OS, however, I was able to import the sqlite3 module because ST relied on the system version of Python. ST3 bundles Python 3.x on Mac OS and I am unable to import sqlite3. Is there any word on that problem? How can I circumvent the problem?

This error comes up when I try import sqlite3 in the ST3 console:

Traceback (most recent call last): File "<string>", line 1, in <module> File "X/sqlite3/__init__.py", line 23, in <module> File "X/sqlite3/dbapi2.py", line 26, in <module> ImportError: No module named '_sqlite3'

0 Likes

PyObjC and ST3?
#2

It’s not compiled in. I’d like to add it, but it’s not currently a high priority, and compiling custom versions of Python is a challenging and time consuming task.

0 Likes

#3

If you want to use that plugin for your very own you probably could extend sys.path to the modules path from your local Python distribution and import modules from there, too.

0 Likes

#4

That might be a good solution. I am still having problems with it though.

  1. Not really essential but: Why don’t I simply get an “No module named ‘sqlite3’” if sqlite3 is not included? Instead I get the error posted above.

  2. When I first add the modules path from your local Python distribution to ST3 and then try to import ‘sqlite3’, I still get an error (see below). In my case the path is ‘/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3’ (PATH variable below).

import sys
sys.path.insert(0, PATH)
import sqlite3

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/sqlite3/__init__.py", line 23, in <module>
    from sqlite3.dbapi2 import *
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/sqlite3/dbapi2.py", line 26, in <module>
    from _sqlite3 import *
ImportError: No module named '_sqlite3'
0 Likes

#5

okay, problem solved. On Mac OX, the code below allows you to import sqlite3 by adding the modules path from your local Python distribution to sys.path. The example uses the location on my system. You can find out which one is the correct location on your system by importing import sqlite3 and import _sqlite3 in your system python and then running print(sqlite3.__file__) and print(_sqlite3.__file__). import sqlite3 works afterwards but maybe the additions to the search path are problematic down the road. I haven’t tested this solution enough.

sys.path.insert(0, '/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload')
sys.path.insert(0, '/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3')
import sqlite3
0 Likes

#6

and if you want to package it, simply copy the sqlite3 folder from /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3 to your ST3 package folder and add _sqlite3.so from /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload to that folder. Now you should be able to import sqlite3 from your package without messing with the sys.path.

0 Likes

#7

My last solution actually does not work for sqlite3 because ‘_sqlite3’ doesn’t get imported (when I tried earlier, the additional path were still part of sys.path). So you can do this:

  1. copy the sqlite3 folder from /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3 to your ST3 package folder
  2. begin your plugin with
    import sys
    sys.path.append(’/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/lib-dynload’)
    import sqlite3

This works on my machine but doesn’t seem to be a good solution for packaging!

Any solution to include ‘_sqlite3.so’ as part of the plugin files to that import _sqlite3?
(import sqlite3 fails because of the problem with import _sqlite3)

0 Likes

#8

I had the same issue with a plugin that we use. It really is a shame that the python included with Sublime is so handicapped - and that it seems nearly impossible to permanently extend the python with e.g. pip or other standard python package tools.

For those interested, we did manage to solve the issue for a range of packages, including sqlite3, crypto and wincrypt encryption, keyring and pyfiglet. All packages with C code simply include the platform-specific binaries. See e.g. github.com/scholer/Mediawiker/tree/master/lib

0 Likes

#9

You might want to check out Package Control’s dependency feature: packagecontrol.io/docs/dependencies

It still needs some polishing that has been happening in the past few weeks and should be released as 3.0.1 soon, but it will surely be usable after that. There are a few example repositories like _ssl that include binaries for all systems.

0 Likes