2013年8月16日金曜日

OS X Mountain Lion + Python 3 + Beautiful Soup 4

OS X Mountain Lion 上で動く Python 3 に Beautiful Soup 4 をインストールしようとしたら嵌ったのでメモ。

Python 3.3.2 (HomeBrew でインストールしたもの)
Beautiful Soup 4.3.0

上手く行ったインストール方法


$ sudo pip3 install beautifulsoup4
$ cd /usr/local/lib/python3.3/site-packages/
$ sudo 2to3-3.3 -w bs4

問題

pip3 でインストールするだけだと Beautiful Soup をインポートするときに SyntaxError 例外が発生する。

uchan@uchan-mba:site-packages$ python3
Python 3.3.2 (default, Jul 11 2013, 15:15:27) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "./bs4/__init__.py", line 392
    print soup.prettify()
             ^
SyntaxError: invalid syntax

2to3-3.3 コマンドを手動で実行することで、ライブラリのソースコードを Python 3.3 向けに修正することができる。(インストーラが自動実行しないのはライブラリのバグかもしれない)

0 件のコメント:

コメントを投稿