virtualenved python无法导入内置的sqlite3

时间:2014-10-30 03:14:09

标签: python sqlite virtualenv

我的操作系统是Linuxmint 17(基本上是Ubuntu 14.04 Trusty),它有一个预安装的python 2.7.6,所以当我这样做时:

$ python -v
>>> import _sqlite3 
>>> # dynamically loaded from /usr/lib/python2.7/lib-dynload/_sqlite3.x86_64-linux-gnu.so

另一方面,每个项目需要,我从源代码编译python 2.7.8,我使用下面的命令创建了一个virtualenv:

virtualenv ~/.venv-python2.7.8/my_project/ -p ~/.localpython-2.7.8/bin/python2.7 --system-site-packages

所以我得到一个孤立的python / pip / django环境,我肯定会得到以下内容:

(my_project)wayne@wayne-linuxmint17 ~/Projects/adsk_devportal $ which python
/home/wayne/.venv-python2.7.8/my_project/bin/python
(my_project)wayne@wayne-linuxmint17 ~/Projects/adsk_devportal $ python --version
Python 2.7.8

但是,在虚拟环境下,我无法导入python built-in sqlite3

$ python -v

>>> import sqlite3
import sqlite3 # directory /home/wayne/.localpython-2.7.8/lib/python2.7/sqlite3
# /home/wayne/.localpython-2.7.8/lib/python2.7/sqlite3/__init__.pyc matches /home/wayne/.localpython-2.7.8/lib/python2.7/sqlite3/__init__.py
import sqlite3 # precompiled from /home/wayne/.localpython-2.7.8/lib/python2.7/sqlite3/__init__.pyc
# /home/wayne/.localpython-2.7.8/lib/python2.7/sqlite3/dbapi2.pyc matches /home/wayne/.localpython-2.7.8/lib/python2.7/sqlite3/dbapi2.py
import sqlite3.dbapi2 # precompiled from /home/wayne/.localpython-2.7.8/lib/python2.7/sqlite3/dbapi2.pyc
# /home/wayne/.localpython-2.7.8/lib/python2.7/collections.pyc matches /home/wayne/.localpython-2.7.8/lib/python2.7/collections.py
import collections # precompiled from /home/wayne/.localpython-2.7.8/lib/python2.7/collections.pyc
dlopen("/home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/_collections.so", 2);
import _collections # dynamically loaded from /home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/_collections.so
dlopen("/home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/operator.so", 2);
import operator # dynamically loaded from /home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/operator.so
# /home/wayne/.localpython-2.7.8/lib/python2.7/keyword.pyc matches /home/wayne/.localpython-2.7.8/lib/python2.7/keyword.py
import keyword # precompiled from /home/wayne/.localpython-2.7.8/lib/python2.7/keyword.pyc
# /home/wayne/.localpython-2.7.8/lib/python2.7/heapq.pyc matches /home/wayne/.localpython-2.7.8/lib/python2.7/heapq.py
import heapq # precompiled from /home/wayne/.localpython-2.7.8/lib/python2.7/heapq.pyc
dlopen("/home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/itertools.so", 2);
import itertools # dynamically loaded from /home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/itertools.so
dlopen("/home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/_heapq.so", 2);
import _heapq # dynamically loaded from /home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/_heapq.so
import thread # builtin
dlopen("/home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/datetime.so", 2);
import datetime # dynamically loaded from /home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/datetime.so
dlopen("/home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/time.so", 2);
import time # dynamically loaded from /home/wayne/.venv-python2.7.8/my_project/lib/python2.7/lib-dynload/time.so
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/wayne/.localpython-2.7.8/lib/python2.7/sqlite3/__init__.py", line 24, in <module>
    from dbapi2 import *
  File "/home/wayne/.localpython-2.7.8/lib/python2.7/sqlite3/dbapi2.py", line 28, in <module>
    from _sqlite3 import *
ImportError: No module named _sqlite3

我可以知道这个的根本原因是什么?解决此问题的最简单方法是什么?

非常感谢任何建议!

1 个答案:

答案 0 :(得分:0)

嗯,

此链接可能有助于https://github.com/NixOS/nixpkgs/issues/492#issuecomment-40740803

不确定是否可以通过更改virtualenv来解决您的问题

diff --git a/virtualenv.py b/virtualenv.py
index 0f5ae79..cfd19bd 100755
--- a/virtualenv.py
+++ b/virtualenv.py
@@ -1191,6 +1191,11 @@ def install_python(home_dir, lib_dir, inc_dir, bin_dir, site_packages, clear, sy
     site_filename_dst = change_prefix(site_filename, home_dir)
     site_dir = os.path.dirname(site_filename_dst)
     writefile(site_filename_dst, SITE_PY)
+    wrapper_path = join(prefix, "lib", py_version, "site-packages")
+    writefile(
+        join(site_dir, 'sitecustomize.py',),
+        "import sys; sys.path.append('%s')" % wrapper_path
+    )
     writefile(join(site_dir, 'orig-prefix.txt'), prefix)
     site_packages_filename = join(site_dir, 'no-global-site-packages.txt')
     if not site_packages: