本机运行正常的代码,部署到服务器上,出现ImportError: No module named _sqlite3
起初以为是pysqlite没装,但是奇怪的是,如果不用virtualenv,是可以import sqlite3的
# python manage.py runserver 0.0.0.0:8080
Performing system checks...
System check identified no issues (0 silenced).
Unhandled exception in thread started by
Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 124, in inner_run
self.check_migrations()
File "/usr/local/lib/python2.7/site-packages/django/core/management/base.py", line 437, in check_migrations
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 20, in __init__
self.loader = MigrationLoader(self.connection)
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 52, in __init__
self.build_graph()
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/loader.py", line 203, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 65, in applied_migrations
self.ensure_schema()
File "/usr/local/lib/python2.7/site-packages/django/db/migrations/recorder.py", line 56, in ensure_schema
with self.connection.schema_editor() as editor:
File "/usr/local/lib/python2.7/site-packages/django/db/backends/sqlite3/schema.py", line 25, in __enter__
self._initial_pragma_fk = c.fetchone()[0]
TypeError: 'NoneType' object has no attribute '__getitem__'
根据错误“self.check_migrations()”和(connections[DEFAULT_DB_ALIAS])来判断,是因为连接数据库的问题。
解决办法:我重新配置了settings.py文件,把里面默认django.db.backends.sqlite3改成了django.db.backends.mysql引擎。然后又安装了MySQL-python。创建了mysql的数据库。最终启动不再报错,顺利执行。
这种情况,一般是安装了多套python,virtualenv使用的那套python没有_sqlite3.so这个文件。
没用virtualenv的时候,使用的是另外一套python的_sqlite3.so
可以先安装sqlite-devel(建议源代码安装sqlite3),然后重新编译virtualenv使用的那套python
编译好后,查看_sqlite3.so这个文件,一般是在这样的目录下 python-2.7.3/lib/python2.7/lib-dynload/_sqlite3.so
方法一:
wget http:
//www.sqlite.org/sqlite-autoconf-3070500.tar.gz
tar xvzf sqlite-autoconf-3070500.tar.gz
cd sqlite-autoconf-3070500
./configure
make
sudo make install
方法二:
sudo yum install sqlite-devel