# How do I get Python virtuelenv and pysqlite to work?



## olav (Jul 21, 2010)

I'm currently playing a bit with the Pylons Python Web Framework and is trying to get it to work with Sqlalchemy and Sqlite.

I have installed py-pysqlite23, py-sqlite3 and python2.6 from ports.

When I'm not in a virtuelenv mode the command
[CMD=""]python -c 'import pysqlite2'[/CMD]
works fine.

But if I create a virtualenv with --no-site-packages
I get this

```
(zbadminenv)[olav@olbsd ~/zbadmin]$ python -c 'import pysqlite2'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named pysqlite2
```

If I try to install it with easy_install I get this error message

```
(zbadminenv)[olav@olbsd ~/zbadmin]$ easy_install pysqlite                                                                             
Searching for pysqlite                                                                                                                
Reading http://www.pylonshq.com/download/                                                                                             
Best match: pysqlite 2.5.6                                                                                                            
Downloading http://cdn.pylonshq.com/download/1.0/pysqlite-2.5.6.tar.gz                                                                
Processing pysqlite-2.5.6.tar.gz                                                                                                      
Running pysqlite-2.5.6/setup.py -q bdist_egg --dist-dir /tmp/easy_install-mrRfW0/pysqlite-2.5.6/egg-dist-tmp-E2BzUt                   
In file included from src/module.c:24:                                                                                                
src/connection.h:33:21: error: sqlite3.h: No such file or directory                                                                   
In file included from src/module.c:24:                                                                                                
src/connection.h:38: error: expected specifier-qualifier-list before 'sqlite3'                                                        
In file included from src/module.c:25:                                                                                                
src/statement.h:37: error: expected specifier-qualifier-list before 'sqlite3'                                                         
src/module.c:265: error: 'SQLITE_OK' undeclared here (not in a function)                                                              
src/module.c:266: error: 'SQLITE_DENY' undeclared here (not in a function)
src/module.c:267: error: 'SQLITE_IGNORE' undeclared here (not in a function)
src/module.c:268: error: 'SQLITE_CREATE_INDEX' undeclared here (not in a function)
src/module.c:269: error: 'SQLITE_CREATE_TABLE' undeclared here (not in a function)
src/module.c:270: error: 'SQLITE_CREATE_TEMP_INDEX' undeclared here (not in a function)
src/module.c:271: error: 'SQLITE_CREATE_TEMP_TABLE' undeclared here (not in a function)
src/module.c:272: error: 'SQLITE_CREATE_TEMP_TRIGGER' undeclared here (not in a function)
src/module.c:273: error: 'SQLITE_CREATE_TEMP_VIEW' undeclared here (not in a function)
src/module.c:274: error: 'SQLITE_CREATE_TRIGGER' undeclared here (not in a function)
src/module.c:275: error: 'SQLITE_CREATE_VIEW' undeclared here (not in a function)
src/module.c:276: error: 'SQLITE_DELETE' undeclared here (not in a function)
src/module.c:277: error: 'SQLITE_DROP_INDEX' undeclared here (not in a function)
src/module.c:278: error: 'SQLITE_DROP_TABLE' undeclared here (not in a function)
src/module.c:279: error: 'SQLITE_DROP_TEMP_INDEX' undeclared here (not in a function)
src/module.c:280: error: 'SQLITE_DROP_TEMP_TABLE' undeclared here (not in a function)
src/module.c:281: error: 'SQLITE_DROP_TEMP_TRIGGER' undeclared here (not in a function)
src/module.c:282: error: 'SQLITE_DROP_TEMP_VIEW' undeclared here (not in a function)
src/module.c:283: error: 'SQLITE_DROP_TRIGGER' undeclared here (not in a function)
src/module.c:284: error: 'SQLITE_DROP_VIEW' undeclared here (not in a function)
src/module.c:285: error: 'SQLITE_INSERT' undeclared here (not in a function)
src/module.c:286: error: 'SQLITE_PRAGMA' undeclared here (not in a function)
src/module.c:287: error: 'SQLITE_READ' undeclared here (not in a function)
src/module.c:288: error: 'SQLITE_SELECT' undeclared here (not in a function)
src/module.c:289: error: 'SQLITE_TRANSACTION' undeclared here (not in a function)
src/module.c:290: error: 'SQLITE_UPDATE' undeclared here (not in a function)
src/module.c:291: error: 'SQLITE_ATTACH' undeclared here (not in a function)
src/module.c:292: error: 'SQLITE_DETACH' undeclared here (not in a function)
src/module.c: In function 'init_sqlite':
src/module.c:419: warning: passing argument 1 of 'PyString_FromString' makes pointer from integer without a cast
error: Setup script exited with error: command 'cc' failed with exit status 1
```
Is it possible to fix this?


----------



## fairy (Jul 21, 2010)

You didn't state why you need --no-site-packages that effectively disables py-sqlite*. Pretty much any python port installs into PYTHON_SITELIBDIR (i.e. /usr/local/lib/python2.6/site-packages).


----------



## olav (Jul 21, 2010)

I want to isolate the project as much as possible, I want to make it as easy as possible to move to another system, upgrade a system and so on. I've been involved with enough of Python projects with library conflicts


----------



## roddierod (Jul 21, 2010)

site-packages is a pretty standard place to put packages, if the packages are in there then you'll start running into library conflicts.

I've written python apps that run on windows, freebsd & linux and always use site-packages without problem.


----------



## puria (Jul 9, 2011)

*Same problem on FreeBSD 8.2*

Almost a year after, same problem here. Seems that since the sqlite3 is in a seperated module, the pythonists freebsd guys just made e little mistake. the _sqlite3.so lib is under "site-packages" system wide that is sad 

So first of all you need to install /usr/ports/databases/py-sqlite3 . Then you can fix your virtualenv --no-site-packages by link in you freshly create virtualenv as this:


```
$ virtualenv --no-site-packages your_virtualenv_name
$ ln -s /usr/local/lib/python2.x/site-packages/_sqlite3.so !$/lib/python2.x/
```

for me is python2.6 but be aware to change your right python version =)


----------

