LeopardにDjango導入

MacOS 10.5(Leopard)にDjangoを導入してみました。以下、アプリケーション生成までのログです。

Djangoインストール

http://www.djangoproject.com/download/」よりDjango-1.0.tar.gzをダウンロードします。
いつものように展開します。
#tar xzvf Django-1.0.tar.gz
#cd Django-1.0
インストールスクリプトを実行します。
#sudo python ./setup.py install

pythonのMySQLdbインストール

ダウンロードしたものを展開して、展開したトップのディレクトリに移動します。
#tar xzvf MySQL-python-1.2.2.tar.gz
#cd MySQL-python-1.2.2
インストールスクリプト実行して構築を行います。
#python ./setup.py build

ところが以下のようなコンパイルエラーが発生しました。

building '_mysql' extension
gcc -DNDEBUG -g -Os -Wall -Wstrict-prototypes -pipe -Dversion_info=(1,2,2,'final',0) -D__version__=1.2.2 -I/usr/local/mysql/include/mysql -I/System/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c _mysql.c -o build/temp.macosx-10.5-i386-2.5/_mysql.o -arch x86_64
In file included from /usr/local/mysql/include/mysql/mysql.h:47,
                 from _mysql.c:40:
/usr/include/sys/types.h:92: error: duplicate ‘unsigned’
/usr/include/sys/types.h:92: error: two or more data types in declaration specifiers
error: command 'gcc' failed with exit status 1
uint型の定義が重複しているようなので,_mysql.cでの定義を以下のようにコメントアウト
//#ifndef uint
//#define uint unsigned int
//#endif      
それで、再度buildとinstallを行いました。
python ./setup.py build
sudo python ./setup.py install

プロジェクトの作成

プロジェクトを作成するパスに移動して。djangoの管理コマンドを実行してプロジェクトを作成します。
#django-admin.py startproject djsite

これでカレントパス以下にdjsiteという名前でプロジェクトが作成されます。

それで、プロジェクトパス内に移動。プロジェクトの管理スクリプトより簡易サーバを起動してみます。
#cd djsite
#python ./manage.py runserver

それでブラウザで「http://localhost:8000/」のように8000ポートのアクセス。「It worked!」とかでれば、とりあえず成功。

DBの初期化

DBの初期化を行います。

作成したプロジェクトフォルダ下にある「settings.py」内にデータベースに関する設定を記述します。あらかじめ作成しているデータベースの情報を指定します。
DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'todo'             # Or path to database file if using sqlite3.
DATABASE_USER = 'root'             # Not used with sqlite3.
DATABASE_PASSWORD = '*******'         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '3306'             # Set to empty string for default. Not used with sqlite3.
これで管理スクリプトでDB同期(syncdb)を実行します。
#python ./manage.py syncdb

ところが、以下のようなエラーとなってしまいました。

Traceback (most recent call last):
  File "./manage.py", line 11, in <module>
    execute_manager(settings)
  File "/Library/Python/2.5/site-packages/django/core/management/__init__.py", line 340, in execute_manager
    utility.execute()
  File "/Library/Python/2.5/site-packages/django/core/management/__init__.py", line 295, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 77, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 95, in execute
    self.validate()
  File "/Library/Python/2.5/site-packages/django/core/management/base.py", line 122, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Python/2.5/site-packages/django/core/management/validation.py", line 22, in get_validation_errors
    from django.db import models, connection
  File "/Library/Python/2.5/site-packages/django/db/__init__.py", line 16, in <module>
    backend = __import__('%s%s.base' % (_import_path, settings.DATABASE_ENGINE), {}, {}, [''])
  File "/Library/Python/2.5/site-packages/django/db/backends/mysql/base.py", line 13, in <module>
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: dynamic module does not define init function (init_mysql)

また。対話インターフェイスからMySQLdbをインポートしても以下のようにエラーが発生します。

python
Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
/Library/Python/2.5/site-packages/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.5/site-packages/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg/_mysql.pyc, but /Users/nyaago/Downloads/MySQL-python-1.2.2 is being added to sys.path
  import sys, pkg_resources, imp
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "MySQLdb/__init__.py", line 19, in <module>
    import _mysql
  File "build/bdist.macosx-10.5-i386/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.5-i386/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_mysql)

これはバイナリのアーキテクチャーの違いのためのようです。mysqlは64bitで構築されていたのが、pythonとMySQLdbのほうは32bitのみでの構築となってたためのエラーのようです。
で、今回はmysqlを32bitで再構築後、python-mysqlも再構築して対応しました。

まず、準備として動作しているMysqlの停止、データのバックアップ、削除を行いました。
#sudo mysqladmin -uroot -p shutdown
#cd /usr/local/mysql
#sudo cp -R data <バックアップ場所>
#sudo rm -rf bin docs info iib libexec share man include sql-bench mysql-test
それから、mysql-5.1.26-rc.tarをダウンロード、展開して以下のように構築しました。
#tar xvf mysql-5.1.26.rc.tar
#cd mysql-5.1.26
#export CFLAGS="-arch i386" 
#export CXXFLAGS="-arch i386" 
#export LDFLAGS="-arch i386"
#sudo ./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/data --with-mysqld-user=mysql --enable-assembler --enable-thread-safe-client --with-charset=utf8 --with-extra-charsets=all --with-innodb --with-ssl -enable-local-infile --enable-shared
#sudo make
#sudo make install
MySQLdbのインストールをやりなおしました。


(おなじような現象でMysqlを入れ直している方(id:shohu33:20080328)がいらっしゃいましたね。)

アプリケーションの生成

#./manage.py startapp todo


とりあえず、ここまで。