Python 3.3 + oursql 0.9.3 導入メモ
いくつか躓いた箇所があったのでメモ.
正しく動作する導入方法
Launchpad上のオフィシャルページからPython 3.x向けのパッケージをダウンロードし,展開する.
$ wget https://launchpad.net/oursql/py3k/py3k-0.9.3/+download/oursql-0.9.3.zip $ unzip oursql-0.9.3.zip
展開したパッケージ内に含まれるoursqlx/oursql.c
(Cythonが生成したコード)を削除する.
$ cd oursql-0.9.3 $ rm oursqlx/oursql.c
ビルトとインストールを実行.正しくimportできることを確認.
$ python setup.py build_ext $ python setup.py install $ python3.3 Python 3.3.0 (default, Sep 29 2012, 22:07:38) [GCC 4.7.2 20120921 (Red Hat 4.7.2-2)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import oursql >>>
上手く動作しない導入方法とその原因
pipから導入
pip
を使った場合,Python 2.x向けのパッケージがダウンロードされてしまう.そのため,setup.py
でSyntax Errorが発生し,導入できない.
$ pip install oursql Downloading/unpacking oursql Downloading oursql-0.9.3.1.tar.gz (164Kb): 164Kb downloaded Running setup.py egg_info for package oursql Traceback (most recent call last): File "<string>", line 14, in <module> File "/.../.virtualenvs/oursql/build/oursql/setup.py", line 53 print "cython not found, using previously-cython'd .c file." ^ SyntaxError: invalid syntax Complete output from command python setup.py egg_info: Traceback (most recent call last): File "<string>", line 14, in <module> File "/.../.virtualenvs/oursql/build/oursql/setup.py", line 53 print "cython not found, using previously-cython'd .c file." ^ SyntaxError: invalid syntax ---------------------------------------- Command python setup.py egg_info failed with error code 1 in /.../.virtualenvs/oursql/build/oursql Storing complete log in /.../.pip/pip.log
ダウンロードしたパッケージをそのまま導入
Launchpadからダウンロードしたzipファイル内には,Cythonで生成された.c
ファイルが同梱されている.そのため,python setup.py build_ext
すると,同梱されている.c
ファイルを利用してビルドが実行される.しかし,同梱されている.c
ファイルはバグの存在する過去のバージョンのPythonを用いて生成されているため,モジュールのimportに失敗してしまう.
$ wget https://launchpad.net/oursql/py3k/py3k-0.9.3/+download/oursql-0.9.3.zip $ unzip oursql-0.9.3.zip $ cd oursql-0.9.3 $ python setup.py build_ext $ python setup.py install $ python3.3 Python 3.3.0 (default, Sep 29 2012, 22:07:38) [GCC 4.7.2 20120921 (Red Hat 4.7.2-2)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import oursql Traceback (most recent call last): File "<stdin>", line 1, in <module> File "oursql.pyx", line 52, in init oursql (oursqlx/oursql.c:23138) ValueError: level must be >= 0 >>>
参考
- Bug #906007 “can't install on python 3” : Bugs : oursql
- Bug #1040093 “Not compatible with python 3” : Bugs : oursql
- Bug #1067767 “py3k branch does not work with python 3.3” : Bugs : oursql
- Issue 15610: PyImport_ImportModuleEx always fails in 3.3 with "ValueError: level must be >= 0" - Python tracker