【Bug】命令行輸入jupyter notebook提示NotImplementedError(Python3.8)

C:\Users\rayna>jupyter notebook
Traceback (most recent call last):
  File "e:\python\python38\lib\runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "e:\python\python38\lib\runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "E:\Python\Python38\Scripts\jupyter-notebook.EXE\__main__.py", line 9, in <module>
  File "e:\python\python38\lib\site-packages\jupyter_core\application.py", line 268, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "e:\python\python38\lib\site-packages\traitlets\config\application.py", line 663, in launch_instance
    app.initialize(argv)
  File "<e:\python\python38\lib\site-packages\decorator.py:decorator-gen-7>", line 2, in initialize
  File "e:\python\python38\lib\site-packages\traitlets\config\application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "e:\python\python38\lib\site-packages\notebook\notebookapp.py", line 1720, in initialize
    self.init_webapp()
  File "e:\python\python38\lib\site-packages\notebook\notebookapp.py", line 1482, in init_webapp
    self.http_server.listen(port, self.ip)
  File "e:\python\python38\lib\site-packages\tornado\tcpserver.py", line 152, in listen
    self.add_sockets(sockets)
  File "e:\python\python38\lib\site-packages\tornado\tcpserver.py", line 165, in add_sockets
    self._handlers[sock.fileno()] = add_accept_handler(
  File "e:\python\python38\lib\site-packages\tornado\netutil.py", line 279, in add_accept_handler
    io_loop.add_handler(sock, accept_handler, IOLoop.READ)
  File "e:\python\python38\lib\site-packages\tornado\platform\asyncio.py", line 99, in add_handler
    self.asyncio_loop.add_reader(fd, self._handle_events, fd, IOLoop.READ)
  File "e:\python\python38\lib\asyncio\events.py", line 503, in add_reader
    raise NotImplementedError
NotImplementedError

On Windows, Tornado requires the WindowsSelectorEventLoop. This is the default in Python 3.7 and older, but Python 3.8 defaults to an event loop that is not compatible with Tornado. Applications that use Tornado on Windows with Python 3.8 must call asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())at the beginning of their main file/function.

這是python3.8中asyncio默認使用ProactorEventLoop造成的,jupyter 依賴tornado,tornado需使用 SelectorEventLoop。而對於像 jupyter 這種依賴於 tornado 的庫,則無法用這種方式。

解決辦法: 修改python安裝目錄(如果忘記安裝在哪了看報錯的路徑信息)\Lib\sitepackages\tornado\platform\asyncio.py文件內容
在這裏插入圖片描述
在asyncio.py文件中加入

import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

在 import asyncio之後手動讓 tornado 選擇SelectorEventLoop,這樣 jupyter 就可以正常使用了。

這時再重新輸入命令就好了,頁面會自動跳轉

C:\Users\rayna>jupyter notebook
[I 22:08:02.710 NotebookApp] JupyterLab extension loaded from e:\python\python38\lib\site-packages\jupyterlab
[I 22:08:02.711 NotebookApp] JupyterLab application directory is e:\python\python38\share\jupyter\lab
[I 22:08:03.660 NotebookApp] Serving notebooks from local directory: C:\Users\rayna
[I 22:08:03.661 NotebookApp] The Jupyter Notebook is running at:
[I 22:08:03.662 NotebookApp] http://localhost:8888/?token=547b9cb38922e7d12fa1f932be6f84ac291ffd4de9723d9a
[I 22:08:03.664 NotebookApp]  or http://127.0.0.1:8888/?token=547b9cb38922e7d12fa1f932be6f84ac291ffd4de9723d9a
[I 22:08:03.665 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 22:08:03.815 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///C:/Users/rayna/AppData/Roaming/jupyter/runtime/nbserver-7948-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=547b9cb38922e7d12fa1f932be6f84ac291ffd4de9723d9a
     or http://127.0.0.1:8888/?token=547b9cb38922e7d12fa1f932be6f84ac291ffd4de9723d9a

在這裏插入圖片描述
Python目前最新版本是2019年10月14日發佈的3.8,從學習與開發角度目前不太建議使用,因爲會出現很多其他版本不會出現的問題,而且現有的解決辦法還很少。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章