Macでpip install sphinxをしたら、Operation not permittedと怒られるのに対処する

MacOS Sierraで、pythonドキュメントビルダー Sphinx をインストールする際に、 six が原因で Operation not permittedエラーが出ることがあります。その対処方法を紹介します。

問題

MacでSphinxをインストールする方法は、pythonが既に入っている場合には、pip install sphinxと打つだけ。

のはずですが、

$ sudo pip install sphinx

としてみると、次のエラーがでて止まることがあります。

  Found existing installation: six 1.4.1
 Uninstalling six:
Cleaning up...
Exception:
Traceback (most recent call last):
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/basecommand.py", line 122, in main
 status = self.run(options, args)
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/commands/install.py", line 283, in run
 requirement_set.install(install_options, global_options, root=options.root_path)
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/req.py", line 1431, in install
 requirement.uninstall(auto_confirm=True)
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/req.py", line 598, in uninstall
 paths_to_remove.remove(auto_confirm)
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/req.py", line 1836, in remove
 renames(path, new_path)
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/util.py", line 295, in renames
 shutil.move(old, new)
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move
 copy2(src, real_dst)
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2
 copystat(src, dst)
 File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat
 os.chflags(dst, st.st_flags)
OSError: [Errno 1] Operation not permitted: '/tmp/pip-mfFcWJ-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

Storing debug log for failure in /Users/beiz/.pip/pip.log

 

Sixのアンインストールに失敗してエラーが出ているようです。

次のようにして、もう一度手動でアンインストールを試みても同じエラーが出ます。

$ sudo pip uninstall six

解決方法

sixが入っているのは無視してsphinxをインストールするよう、次のようにpipのオプションで指定します。

sudo pip install sphinx --upgrade --ignore-installed six

この方法で、無事インストールすることが出来ました。

Successfully installed sphinx six babel sphinxcontrib-websupport Jinja2 alabaster imagesize setuptools requests Pygments snowballstemmer docutils typing pytz MarkupSafe certifi idna chardet urllib3
Cleaning up...

まとめ

インストール済みの six が邪魔をして、pip install sphinx が通らないことがあります。

–ignore-installed オプションでsixを指定してみると、Sphinx のインストールができました。

以上、Macでpip install sphinxをしたら、Operation not permittedと怒られるのに対処する。でした。