Mac に Python+Django の開発環境を構築 [python][django]

Mac で Python/Django の開発環境を作ったメモ / イッキログ Tech を参考に作業開始。

まずは、Mac Ports のインストール。
The MacPorts Project Official Homepage から Snow Leopard 向けの dmg をダウンロードしてインストールします。

その後、パスの設定。下記を .zshrc に追記し、source .zshrc

export PATH=/opt/local/bin:/opt/local/sbin:$PATH


次に Python をインストール。

% sudo port install python26
---> Computing dependencies for python26
---> Dependencies to be installed: bzip2 db46 gdbm gettext expat libiconv gperf ncurses ncursesw openssl zlib readline sqlite3
---> Fetching bzip2
---> Attempting to fetch bzip2-1.0.6.tar.gz from http://distfiles.macports.org/bzip2
---> Verifying checksum(s) for bzip2
---> Extracting bzip2
---> Applying patches to bzip2
---> Configuring bzip2
---> Building bzip2
---> Staging bzip2 into destroot
---> Installing bzip2 @1.0.6_0
---> Activating bzip2 @1.0.6_0
---> Cleaning bzip2
---> Fetching db46
---> Attempting to fetch patch.4.6.21.1 from http://download.oracle.com/berkeley-db/patches/db/4.6.21/
---> Attempting to fetch patch.4.6.21.2 from http://download.oracle.com/berkeley-db/patches/db/4.6.21/
---> Attempting to fetch patch.4.6.21.3 from http://download.oracle.com/berkeley-db/patches/db/4.6.21/
---> Attempting to fetch patch.4.6.21.4 from http://download.oracle.com/berkeley-db/patches/db/4.6.21/
---> Attempting to fetch db-4.6.21.tar.gz from http://download-uk.oracle.com/berkeley-db/
---> Verifying checksum(s) for db46
---> Extracting db46
---> Applying patches to db46
---> Configuring db46
Error: db46 requires the Java for Mac OS X development headers.
Error: Download the Java Developer Package from: <https://connect.apple.com/cgi-bin/WebObjects/MemberSite.woa/wa/getSoftware?bundleID=20719>
Error: Target org.macports.configure returned: missing Java headers
Log for db46 is at: /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_ports_databases_db46/main.log
Error: The following dependencies failed to build: db46 gdbm gettext expat libiconv gperf ncurses ncursesw openssl zlib readline sqlite3
Error: Status 1 encountered during processing.
To report a bug, see <http://guide.macports.org/#project.tickets>

とエラーになりました。

Java headers の問題のようなので

% sudo ln -s /Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Headers /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Headers

で、リンクを張って対応しました。

再度 Python をインストールします。

% sudo port install python26
% sudo port install python_select
% sudo python_select python26


次に、MySQL をインストールします。
MySQLのサイト から、Mac OS X ver. 10.6 (x86, 32-bit), DMG Archive をダウンロードし、インストールします。

次に、virtualenv をインストールします。
Python開発環境 / aodag's posterous を参考に。
Package Index > virtualenv 1.5.1 から virtualenv をダウンロードし、virtualenv.py を取り出します。

% tar jxvfp virtualenv-1.5.1.tar.gz virtualenv-1.5.1/virtualenv.py
% mv virtualenv-1.5.1/virtualenv.py ~/bin
% rm -rf virtualenv-1.5.1

<!--


環境を構築していきます。

% python ~/bin/virtualenv.py --no-site-packages --distribute newproject
% bin/pip install zc.buildout
% bin/buildout init
% vi
[buildout]
parts =
    paster
[paster]
recipe = zc.recipe.egg
eggs = PasteScript
% bin/buildout

-->

Developing Django apps with zc.buildout / Jacob Kaplan-Moss を参考に、環境を構築していきます。

% mkdir -p myproject/src/myproject
% cd myproject
% touch LICENSE README
% wget 'http://svn.zope.org/*checkout*/zc.buildout/trunk/bootstrap/bootstrap.py'
% cat - > buildout.cfg
[buildout]
parts =
^D
% touch setup.py
% touch src/myproject/__init__.py
% python bootstrap.py
% ./bin/buildout
% cat - > setup.py
from setuptools import setup, find_packages

setup(
    name = "myproject",
    version = "1.0",
    url = 'http://github.com/example/myproject,
    license = 'BSD',
    description = "An example for Django apps.",
    author = 'my name,
    packages = find_packages('src'),
    package_dir = {'': 'src'},
    install_requires = ['setuptools'],
)
^D
% cat - > buildout.cfg
[buildout]
parts = python
develop = .
eggs = myproject

[python]
recipe = zc.recipe.egg
interpreter = python
eggs = ${buildout:eggs}
^D
% ./bin/buildout
cat - > src/myproject/testsettings.py
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = '/tmp/myproject.db'
INSTALLED_APPS = ['myproject]
ROOT_URLCONF = ['myproject.urls']
^D
% touch src/myproject/models.py
% vi buildout.cfg
[buildout]
parts = python django
develop = .
eggs = myproject

[python]
recipe = zc.recipe.egg
interpreter = python
eggs = ${buildout:eggs}

[django]
recipe = djangorecipe
version = trunk
project = myproject
projectegg = myproject
settings = testsettings
test = myproject
eggs = ${buildout:eggs}

% ./bin/buildout