导入 asyncio 抛出错误

Importing asyncio throws error

在我的终端中,我想用 asyncio 测试一些东西。这是我所做的:

$ python3.6
Python 3.6.2 (v3.6.2:5fd33b5926, Jul 16 2017, 20:11:06) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio

这引发了如下错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/__init__.py", line 42, in <module>
tasks.__all__ +
AttributeError: module 'asyncio.tasks' has no attribute '__all__'

为什么会抛出这个错误,我该如何解决? (我以同样的方式检查了我的 python 3.5 解释器并且没有出现错误,所以可能库已损坏?)

我已经 运行 Python 使用了 -v 开关,在 运行ning import asyncio 之后在提示符下产生的输出相当大,所以它在 this GitHub gist.

中可用

好像是个bug

尝试通过 brew

升级您的 Python
brew upgrade python3

目前Python3.6.5可用,没有这个问题

您的本地安装已损坏。从您提供的 python -v 输出:

# bytecode is stale for 'asyncio.tasks'
# code object from /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py
import 'asyncio.tasks' # <_frozen_importlib_external.SourceFileLoader object at 0x104cf7860>

字节码陈旧 消息表示 asyncio/tasks.py 文件比随附的 asyncio/__pycache__/tasks.cpython-36.pyc 文件更新。这表明某些东西改变了 tasks.py 文件,导致内容与 Python 二进制文件附带的内容不同。

为了比较,同级模块 asyncio.events 是从 Python 安装程序在安装时提供的字节码缓存中加载的:

# code object from '/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/__pycache__/events.cpython-36.pyc'
import 'asyncio.events' # <_frozen_importlib_external.SourceFileLoader object at 0x104ccf4e0>

该模块的代码对象是从 asyncio/__pycache__/events.cpython-36.pyc 文件加载的。

您可以从 OS X installer, but at this point I'd just grab the newer 3.6.5 release 重新安装 Python。

您也可以尝试通过下载 original source from the v3.6.2 tag 来恢复原始内容,但是您必须确保重新生成字节码 (运行 sudo python -m compileall /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py)并且您需要检查任何其他此类更改的文件(尝试 find /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6 -name \*.py -newer /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/__init__.py