简体   繁体   English

导入异步抛出错误

[英]Importing asyncio throws error

In my terminal, I wanted to test something with asyncio. 在终端中,我想用asyncio测试某些东西。 Here's what I did: 这是我所做的:

$ 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

And this threw an error as follows: 这引发了如下错误:

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__'

Why does it throw this error, and how can I fix it? 为什么会引发此错误,我该如何解决? (I checked in my python 3.5 interpreter the same way and got no error, so maybe the library got corrupted?) (我以相同的方式检查了我的python 3.5解释器,但没有错误,所以库可能已损坏?)

I've run Python with the -v switch, the output produced after running import asyncio at the prompt is rather large, so it is available in this GitHub gist . 我已经使用-v开关运行了Python,在提示符下运行import asyncio之后产生的输出相当大,因此可以在GitHub gist中使用

Your local installation has been corrupted. 您的本地安装已损坏。 From the python -v output you provided: 从您提供的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>

The bytecode is stale message means that asyncio/tasks.py file is newer than the accompanying asyncio/__pycache__/tasks.cpython-36.pyc file. 字节码是陈旧消息意味着asyncio/tasks.py文件比伴随 asyncio/__pycache__/tasks.cpython-36.pyc文件。 This indicates that something has altered the tasks.py file, causing the contents to be different from what was shipped with your Python binary. 这表明某些内容已更改tasks.py文件,从而导致其内容与Python二进制文件附带的内容不同。

For comparison, the sibling module asyncio.events was loaded from the bytecode cache, which was provided by the Python installer at install time: 为了进行比较,兄弟模块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>

The code object for that module was loaded from the asyncio/__pycache__/events.cpython-36.pyc file. 该模块的代码对象是从asyncio/__pycache__/events.cpython-36.pyc文件加载的。

You could re-install Python from the OS X installer , but at this point I'd just grab the newer 3.6.5 release instead. 您可以从OS X安装程序中重新安装Python,但此时,我只想获取较新的3.6.5版本

You could also try to re-instate the original contents by downloading the original source from the v3.6.2 tag , but then you'll have to make sure the bytecode is regenerated (run sudo python -m compileall /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py ) and you'll need to check for any other such changed files (try 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 ) 您还可以尝试通过从v3.6.2标记下载原始源来恢复原始内容,但随后必须确保重新生成字节码(运行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

It seems that it's a bug 看来是个错误

Try to upgrade your Python via brew 尝试通过brew升级Python

brew upgrade python3

Currently, Python 3.6.5 is available and there is no such problem 目前,Python 3.6.5可用,并且没有这样的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM