django初體驗之Error: No module named _sqlite
安裝好django之後,在bash shell中使用django-admin.py建立新專案,直接執行django-admin.py或django-admin而不帶任何選項,則會列舉出所有的選項。選項startproject用於建立一個新的專案。
[[email protected]]# django-admin.py startproject mysite
[[email protected]]# cd mysite
[[email protected]]# ls -l
-rwxr-xr-x. 1 root root 249 Dec 31 16:45 manage.py
drwxr-xr-x. 2 root root 4096 Dec 31 16:58 mysite
[ [email protected]]# cd mysite
[[email protected]]# ls -l
-rw-r--r--. 1 root root 0 Dec 31 16:45 __init__.py
-rw-r--r--. 1 root root 3164 Dec 31 16:45 settings.py
-rw-r--r--. 1 root root 818 Dec 31 16:45 urls.py
-rw-r--r--. 1 root root 389 Dec 31 16:45 wsgi.py
下面準備runserver,給“manage.py”新增執行許可權,然後執行“./manage.py runserver”,這時又遇到問題,traceback的結尾顯示:
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3
google之後得知需要安裝pysqlite。訪問https://pypi.python.org/simple/pysqlite/ 可以下載pysqlite的原始碼
[[email protected]]# wget https://pypi.python.org/packages/source/p/pysqlite/pysqlite-2.8.1.tar.gz
[[email protected]]# tar -zxf pysqlite-2.8.1.tar.gz
[[email protected]]# cd pysqlite-2.8.1
[[email protected]]# python setup.py
按以上命令安裝pysqlite之後收到如下報錯:
building 'pysqlite2._sqlite' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -DMODULE_NAME="pysqlite2.dbapi2" -IPackage -Isqlite3 -Iwas -Inot -Ifound -Iin -Ithe -Ipkg-config -Isearch -Ipath. -IPerhaps -Iyou -Ishould -Iadd -Ithe -Idirectory -Icontaining -I`sqlite3.pc' -Ito -Ithe -IPKG_CONFIG_PATH -Ienvironment -Ivariable -INo -Ipackage -I'sqlite3' -Ifound -I/usr/local/include/python2.7 -c src/module.c -o build/temp.linux-x86_64-2.7/src/module.o
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: In function ‘module_complete’:
src/module.c:101: warning: implicit declaration of function ‘sqlite3_complete’
src/module.c: In function ‘init_sqlite’:
src/module.c:400: warning: implicit declaration of function ‘sqlite3_libversion’
src/module.c:400: warning: passing argument 1 of ‘PyString_FromString’ makes pointer from integer without a cast
/usr/local/include/python2.7/stringobject.h:63: note: expected ‘const char *’ but argument is of type ‘int’
error: command 'gcc' failed with exit status 1
真是奇了怪了,明明有gcc嘛,繼續google,得知是缺少libffi-devel python-devel openssl-devel三個軟體,(有的說是隻缺少python-devel,我三個都yum了)
繼續,還是同樣報錯,一怒之下,使用“yum -y install gcc*”把所有以gcc開頭的軟體包都裝了,再次嘗試,仍然報錯。
冷靜下來仔細往回看,發現若干“src/connection.h:33:21: error: sqlite3.h: No such file or directory”之類的訊息,最後終於確定是缺少了“sqlite-devel”這個軟體包。安裝之後,再次嘗試,終於build和install成功。
再次執行“./manage.py runserver”,仍然提示
django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3
突然想起前面安裝了很多devel軟體包,應該是需要重新編譯Python,抱著試一試的想法重新編譯安裝了Python2.7,然後回來執行“./manage.py runserver”,終於成功!
[[email protected]]# ./manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
You have unapplied migrations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
January 04, 2016 - 10:50:31
Django version 1.9, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.