简体   繁体   中英

Importing asyncio throws error

In my terminal, I wanted to test something with 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?)

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 .

Your local installation has been corrupted. From the python -v output you provided:

# 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. This indicates that something has altered the tasks.py file, causing the contents to be different from what was shipped with your Python binary.

For comparison, the sibling module asyncio.events was loaded from the bytecode cache, which was provided by the Python installer at install time:

# 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.

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.

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 )

It seems that it's a bug

Try to upgrade your Python via brew

brew upgrade python3

Currently, Python 3.6.5 is available and there is no such problem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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