python3.8 安装 jupyter PyQt5

本文介绍了在Python 3.8中安装Jupyter和PyQt5时遇到的问题,包括Python更新特性,如新的表达式和Positional-only函数参数。在安装过程中,由于Windows上的asyncio默认使用ProactorEventLoop,导致jupyter依赖的tornado冲突。为了解决这个问题,需要手动选择SelectorEventLoop。文章提供了临时解决方案并链接了相关issue。
摘要由CSDN通过智能技术生成

python 3.8 更新简介

  1. 新的 := 表达式
  2. Positional-only 函数参数
  3. 更易于调试的 f-string
  4. asyncio 在 windows 上默认使用 ProactorEventLoop 而不是之前的 SelectorEventLoop
  5. 等等
    详情参考 python3.8 官方文档

安装 jupyter 与 PyQt5

首先去 Unofficial Windows Binaries for Python Extension Packages 下载需要的 whl 包手动安装 (因为如果本地没有这些包的编译工具,pip 安装过程中会报错)
将以下包下载到本地之后:

  1. pywin32‑225‑cp38‑cp38‑win_amd64.whl
  2. pyzmq‑18.1.0‑cp38‑cp38‑win_amd64.whl
  3. pywinpty‑0.5.5‑cp38‑cp38‑win_amd64.whl

cmd 到文件下载目录之后执行以下命令手动安装:

pip install pywin32‑225‑cp38‑cp38‑win_amd64.whl pyzmq‑18.1.0‑cp38‑cp38‑win_amd64.whl pywinpty‑0.5.5‑cp38‑cp38‑win_amd64.whl
pip install -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com jupyter PyQt5

D:\Python3\Lib\site-packages\pywin32_system32 下的两个文件,也就是 pythoncom38.dllpywintypes38.dll 拷贝到 C:\Windows\System32, 否则执行 jupyter-qtconsole.exe 将会报以下错误:

import win32api ImportError: DLL load failed: 找不到指定的模块”

运行 jupyter-notebook 之后,会报以下错误:

C:\Python38\Scripts>jupyter-notebook.exe
Traceback (most recent call last):
  File "c:\python38\lib\runpy.py", line 192, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\python38\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Python38\Scripts\jupyter-notebook.exe\__main__.py", line 9, in <module>
  File "c:\python38\lib\site-packages\jupyter_core\application.py", line 267, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "c:\python38\lib\site-packages\traitlets\config\application.py", line 663, in launch_instance
    app.initialize(argv)
  File "<c:\python38\lib\site-packages\decorator.py:decorator-gen-7>", line 2, in initialize
  File "c:\python38\lib\site-packages\traitlets\config\application.py", line 87, in catch_config_error
    return method(app, *args, **kwargs)
  File "c:\python38\lib\site-packages\notebook\notebookapp.py", line 1679, in initialize
    self.init_webapp()
  File "c:\python38\lib\site-packages\notebook\notebookapp.py", line 1442, in init_webapp
    self.http_server.listen(port, self.ip)
  File "c:\python38\lib\site-packages\tornado\tcpserver.py", line 152, in listen
    self.add_sockets(sockets)
  File "c:\python38\lib\site-packages\tornado\tcpserver.py", line 165, in add_sockets
    self._handlers[sock.fileno()] = add_accept_handler(
  File "c:\python38\lib\site-packages\tornado\netutil.py", line 279, in add_accept_handler
    io_loop.add_handler(sock, accept_handler, IOLoop.READ)
  File "c:\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 "c:\python38\lib\asyncio\events.py", line 501, in add_reader
    raise NotImplementedError
NotImplementedError

是由于 python3.8 asyncio 在 windows 上默认使用 ProactorEventLoop 造成的,而不是之前的 SelectorEventLoop。jupyter 依赖 tornado,而 tornado 在 window 上需要使用 SelectorEventLoop,所以产生这个报错

对于用户自己写的,依赖于 tornado 的程序,tornado 官方文档已经说明:

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.

而对于像 jupyter 这种依赖于 tornado 的库,则无法用这种方式。目前这个问题已由 python 官方跟踪:https://bugs.python.org/issue37373

临时解决办法:

修改 D:\Python3\Lib\site-packages\tornado\platform\asyncio.py

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

也就是在 import asyncio 之后,手动让 tornado 选择 SelectorEventLoop,这样 jupyter 就可以正常使用了。

所以现在其实不太建议升级到 python3.8

相关 issue:

  1. https://github.com/tornadoweb/tornado/issues/2608
  2. https://github.com/jupyter/notebook/issues/4613
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值