Python2.6からsqlite3が読み込まれないエラーの解決方法

ソースからインストールしたpython2.6で、Djangoのsyncdb等を使おうとすると、

sqlite3が読み込めないとエラーが発生します。

% python manage.py syncdb
raise ImproperlyConfigured, "Error loading %s module: %s" % (module, exc)
django.core.exceptions.ImproperlyConfigured: Error loading sqlite3 module: No module named _sqlite3

通常のコマンドラインからも確認してみます。

% python
Python 2.6.6 (r266:84292, Jan  3 2011, 14:20:44)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.6/sqlite3/__init__.py", line 24, in <module>
    from dbapi2 import *
  File "/usr/local/lib/python2.6/sqlite3/dbapi2.py", line 27, in <module>
    from _sqlite3 import *
ImportError: No module named _sqlite3

やっぱり呼び出し出来ないとエラーが出ます。


先人から学ぶ

同じような問題に取り組んでいる先人がいらっしゃいます。



python2.6の標準モジュールであるはずのsqlite3をimportしたらエラーになる場合の対処法



なるほどPython2.6だとsqlite3はインストール標準から外れるのか。

解決方法


方法としては別にsqliteをインストールして、Python2.6を再インストールするやり方があります。



sqliteを使えるように「yum -y install sqlite-devel」とインストールして、Pythonをソースからインストールし直せばOKです。

% python2.6
Python 2.6.6 (r266:84292, Jan  3 2011, 14:20:44)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
おまけ djangoのsyncdb

冒頭のsyncdbが通るようになります。

% python manage.py syncdb
Creating table auth_permission
Creating table auth_group
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site
Creating table notification_noticetype
Creating table notification_noticesetting
Creating table notification_notice
Creating table notification_noticequeuebatch
Creating table notification_observeditem
Creating table emailconfirmation_emailaddress
Creating table emailconfirmation_emailconfirmation
Creating table mailer_message
Creating table mailer_dontsendentry
Creating table mailer_messagelog
Creating table announcements_announcement
Creating table basic_profiles_profile
Creating table account_account
Creating table account_otherserviceinfo
Creating table account_passwordreset
Creating table signup_codes_signupcode
Creating table signup_codes_signupcoderesult
Creating table django_admin_log
Creating table django_openid_nonce
Creating table django_openid_association
Creating table django_openid_useropenidassociation

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'hogehoge'):
E-mail address:
Error: That e-mail address is invalid.
E-mail address:
Password:
Password (again):
Superuser created successfully.
Installing index for auth.Permission model
Installing index for auth.Message model
Installing index for notification.NoticeSetting model
Installing index for notification.Notice model
Installing index for notification.ObservedItem model
Installing index for emailconfirmation.EmailAddress model
Installing index for emailconfirmation.EmailConfirmation model
Installing index for announcements.Announcement model
Installing index for account.OtherServiceInfo model
Installing index for account.PasswordReset model
Installing index for signup_codes.SignupCode model
Installing index for signup_codes.SignupCodeResult model
Installing index for admin.LogEntry model
Installing index for django_openid.UserOpenidAssociation model

以上で完了です。