4

There is a sqlite3 library that comes with python 2.7.3, but it is hardly the latest version.

I would like to upgrade it within a virtualenv environment. In other words, the upgrade only applies to the version of python installed within this virtualenv.

What is the correct way to do so?

skyork
  • 7,113
  • 18
  • 63
  • 103
  • Out of time for now: it should be possible by using the `pysqlite` package, as long as it'll pick up the newer sqlite version when being compiled. The virtual env would need to be extended to point to the new sqlite library when active. – Martijn Pieters Jan 26 '13 at 22:43

3 Answers3

4

The below works for me, but please comment if there is any room for improvement:

  1. Activate the virtualenv to which you are going to install the latest sqlite3

  2. Get the latest source of pysqlite package from google code: wget http://pysqlite.googlecode.com/files/pysqlite-2.6.3.tar.gz

  3. Compile pysqlite from source and together with the latest sqlite database: python setup.py build_static

  4. Install it to the site-packages directory of the virtualenv: python setup.py install

  5. The above will actually install the pysqlite into path-to-virtualenv/lib/python2.7/site-packages, which is where all other pip-installed libraries are.

Now, I have the latest version of sqlite (compiled into pysqlite) installed within a virtualenv, so I can do: from pysqlite2 import dbapi2 as sqlite

skyork
  • 7,113
  • 18
  • 63
  • 103
  • thank you so much!!! this fixed my "cannot import name utils" error when trying to use sqlite in django – Kevin King Aug 21 '13 at 10:08
  • The Google Code link is no longer valid. It can be found at: `https://pypi.python.org/packages/source/p/pysqlite/pysqlite-2.6.3.tar.gz` – Drewness Apr 29 '15 at 20:12
  • 2
    Note that pysqlite is Python 2 only. Python 3 folks are out of luck. – Nelson Mar 18 '16 at 16:45
4

I checked setting something like this, works:

export export LD_LIBRARY_PATH=$HOME/<your-sqlite-install-dir>/sqlite3/lib

I’ve added it next to the line export PATH in the activate file:

PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH
export LD_LIBRARY_PATH=$HOME/…/sqlite3/lib  # <- Here

One can check it in either one of two ways.

From Python in the virtualenv, first do:

>>> import _sqlite3
>>> _sqlite3.__file__
'/usr/lib/…/_sqlite3.cpython-35m-i386-linux-gnu.so'

Then exit Python and run ldd on the string returned:

$ ldd /usr/lib/…/_sqlite3.cpython-35m-i386-linux-gnu.so
> …
> libsqlite3.so.0 => /home/…/sqlite3/lib/libsqlite3.so.0
> …

Or alternatively, again in Python from the virtualenv:

>>> import sqlite3
>>> sqlite3.sqlite_version
'3.21.0'  # Was 3.11.8 before
Hibou57
  • 6,870
  • 6
  • 52
  • 56
1

I was stuck in the same problem once. This solved it for me:

  1. Download and untar the python version required
  2. mkdir local
  3. untar sqlite after downloading its package
  4. ./configure --prefix=/home/aanuj/local
  5. make
  6. make install
  7. ./configure --prefix=/home/anauj/local LDFLAGS='-L/home/aaanuj/local/lib' CPPFLAGS='-I/home/aanuj/local/include'
  8. make
  9. Find the sqlite3.so and copy to home/desired loc
  10. Extract beaver
  11. Setup the virtual env with the python version needed
  12. Activate the env
  13. unalias python
  14. export PYTHONPATH=/home/aanuj(location of _sqlite3.so)
  15. Enjoy
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47
  • 1
    But then testing `sqlite3.sqlite_version`, it still returns the old version. – Hibou57 Dec 24 '17 at 18:33
  • This topic contradicts the idea to use PYTHONPATH: https://stackoverflow.com/questions/1099981/why-cant-python-find-shared-objects-that-are-in-directories-in-sys-path – Hibou57 Dec 24 '17 at 18:37