【问题标题】:ImportError: cannot import name 'Deque'ImportError:无法导入名称“双端队列”
【发布时间】:2020-05-17 13:09:37
【问题描述】:

我正在尝试在 Heroku 上托管我的 Discord.py 机器人。当我的机器人启动时出现以下错误:

2020-01-31T20:49:31.501236+00:00 heroku[worker.1]: State changed from starting to up
2020-01-31T20:49:32.858327+00:00 heroku[worker.1]: State changed from up to crashed
2020-01-31T20:49:32.839912+00:00 heroku[worker.1]: Process exited with status 1
2020-01-31T20:49:32.766672+00:00 app[worker.1]: Traceback (most recent call last):
2020-01-31T20:49:32.766696+00:00 app[worker.1]:   File "bot.py", line 3, in <module>
2020-01-31T20:49:32.766815+00:00 app[worker.1]:     import discord
2020-01-31T20:49:32.766819+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/__init__.py", line 23, in <module>
2020-01-31T20:49:32.766952+00:00 app[worker.1]:     from .client import Client
2020-01-31T20:49:32.766954+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/discord/client.py", line 35, in <module>
2020-01-31T20:49:32.767061+00:00 app[worker.1]:     import websockets
2020-01-31T20:49:32.767062+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/websockets/__init__.py", line 3, in <module>
2020-01-31T20:49:32.767164+00:00 app[worker.1]:     from .auth import *
2020-01-31T20:49:32.767166+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/websockets/auth.py", line 15, in <module>
2020-01-31T20:49:32.767263+00:00 app[worker.1]:     from .server import HTTPResponse, WebSocketServerProtocol
2020-01-31T20:49:32.767265+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/websockets/server.py", line 49, in <module>
2020-01-31T20:49:32.767374+00:00 app[worker.1]:     from .protocol import WebSocketCommonProtocol
2020-01-31T20:49:32.767375+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.6/site-packages/websockets/protocol.py", line 18, in <module>
2020-01-31T20:49:32.767487+00:00 app[worker.1]:     from typing import (
2020-01-31T20:49:32.767492+00:00 app[worker.1]: ImportError: cannot import name 'Deque'

我该如何解决这个问题?

【问题讨论】:

  • @ChrisCharley 这个错误出现在 websocket 库代码中,它正在寻找输入的 Deque 对象,而不是实际的集合本身。

标签: python python-3.x heroku discord discord.py


【解决方案1】:

我猜你在 Heroku 上运行的 Python 版本没有typing.Deque。尝试运行 heroku run -a &lt;app_name&gt; python -V 或运行 python shell 并在测功机上尝试导入自己。如果是 python 版本问题,那么您应该能够通过在项目根目录中定义 runtime.txt 文件并在此处显式设置版本来解决它 (https://devcenter.heroku.com/articles/python-runtimes)

【讨论】:

    【解决方案2】:

    websockets 模块与 python 3.6.0 不兼容

    我自己重现了这个问题:

    C:\Users\Me>python
    Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    
    C:\Users\Me>pip install websockets
    Collecting websockets
      Downloading https://files.pythonhosted.org/packages/6a/bd/8555c65b3fca7310b2601d0957b36511c3c3c2e8a93a6d3f90c671b70c6a/websockets-8.0.2-cp36-cp36m-win_amd64.whl (65kB)
         |████████████████████████████████| 71kB 918kB/s
    Installing collected packages: websockets
    Successfully installed websockets-8.0.2
    WARNING: You are using pip version 19.1.1, however version 20.0.2 is available.
    You should consider upgrading via the 'python -m pip install --upgrade pip' command.
    
    C:\Users\Me>python
    Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import websockets
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\websockets\__init__.py", line 3, in <module>
        from .auth import *
      File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\websockets\auth.py", line 15, in <module>
        from .server import HTTPResponse, WebSocketServerProtocol
      File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\websockets\server.py", line 49, in <module>
        from .protocol import WebSocketCommonProtocol
      File "C:\Users\Me\AppData\Local\Programs\Python\Python36\lib\site-packages\websockets\protocol.py", line 18, in <module>
        from typing import (
    ImportError: cannot import name 'Deque'
    

    它适用于 3.6.1: https://repl.it/repls/NotableDarkorchidBloatware

    【讨论】:

    • 通过将python版本更改为3.7.2解决!谢谢。
    猜你喜欢
    • 2017-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 2014-10-10
    • 2014-09-20
    • 2014-08-28
    • 2014-06-10
    • 2016-05-16
    相关资源
    最近更新 更多