A bolt out of the blue

競プロ、その他勉強したことなど

iPythonを起動しようとするとModuleNotFoundError

つまづき

競プロに参加しようとして、いつものようにipythonを起動しようとしたら、なぜか動かなくなっていた。

$ ipython
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/bin/ipython", line 7, in <module>
    from IPython import start_ipython
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/__init__.py", line 55, in <module>
    from .terminal.embed import embed
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/terminal/embed.py", line 16, in <module>
    from IPython.terminal.interactiveshell import TerminalInteractiveShell
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/terminal/interactiveshell.py", line 20, in <module>
    from prompt_toolkit.formatted_text import PygmentsTokens
ModuleNotFoundError: No module named 'prompt_toolkit.formatted_text'

"ModuleNotFoundError: No module named 'prompt_toolkit.formatted_text'"で調べると、 Jupyter-console incompatible with prompt-toolkit 2.0.2 · Issue #158 · jupyter/jupyter_console · GitHub

どうも、2系にアップデートされたprompt-toolkitに6系のipythonが対応していないらしい。 jupyteripythonで必要なprompt-toolkitのバージョンがコンフリクトしているようだ。

解決方法

最新のパッケージでは依存関係の問題は解決されているので、いずれも最新バージョンにアップデートすれば動く。

仮想環境を作り直す or キャッシュ無効にしてインストールし直せば良さそうなのだが、 jupyterの依存パッケージが多いので、一から作り直した。

$ rm -rf venv
$ python3 -m venv venv
$ pip list
Package    Version
---------- -------
pip        10.0.1 
setuptools 39.0.1 
$ pip install ipython jupyter
Collecting ipython
  Using cached https://files.pythonhosted.org/packages/a0/27/29d66ed395a5c2c3a912332d446a54e2bc3277c36b0bbd22bc71623e0193/ipython-7.0.1-py3-none-any.whl
Collecting pygments (from ipython)
...
Successfully installed MarkupSafe-1.0 Send2Trash-1.5.0 bleach-3.0.0 defusedxml-0.5.0 entrypoints-0.2.3 ipykernel-5.0.0 ipywidgets-7.4.2 jinja2-2.10 jsonschema-2.6.0 jupyter-1.0.0 jupyter-client-5.2.3 jupyter-console-6.0.0 jupyter-core-4.4.0 mistune-0.8.3 nbconvert-5.4.0 nbformat-4.4.0 notebook-5.7.0 pandocfilters-1.4.2 prometheus-client-0.4.0 python-dateutil-2.7.3 pyzmq-17.1.2 qtconsole-4.4.1 terminado-0.8.1 testpath-0.4.2 tornado-5.1.1 webencodings-0.5.1 widgetsnbextension-3.4.2
$ ipython
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 26 2018, 23:26:24) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.0.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]:   

補足

どうやら、pipにはまだ依存関係を正しく解決するリゾルバ機能がないため、環境をアレコレ触っていると事故るみたい。

イシューはこちら: Pip needs a dependency resolver · Issue #988 · pypa/pip · GitHub