Error with the asyncio module

Asked

Viewed 86 times

2

I’m trying to run the example below in IDLE (Python 3.43, Windows 7, 64bits), there are days, but always gives an error. I tried to search for solution on the English version site, on google, and found no answer. I can run on Anaconda, but on IDLE it does not work. The asyncio module is already installed. I’ll put the code and then the error:

import asyncio

def print_and_repeat(loop):
    print('Hello World')
    loop.call_later(2, print_and_repeat, loop)

loop = asyncio.get_event_loop()
loop.call_soon(print_and_repeat, loop)
loop.run_forever()

Error:

Traceback (most recent call last):   
  File   "C:\Users\Gustavo\Desktop\asyncio.py", line 1, in <module>  
    import asyncio   File "C:\Users\Gustavo\Desktop\asyncio.py", line 7, in <module>  
loop = asyncio.get_event_loop() AttributeError: 'module' object has no attribute 'get_event_loop'
  • Here on my machine ran normally.. strange. Both on the console and on IDLE. Did you ever run on the console? PS: I have installed version 3.4.0.

  • I still don’t know. While running the example, it gave error, but created a folder with the name pycache. Inside it, I ran with two clicks (left button) the asyncio.cpython-34 file that was created (in the previous IDLE run) and it ran without any error in c: Windows py.exe. I don’t know what it means, giving error in the original file, and running in its cache.

1 answer

2

If you called your file asyncio.py it has more priority to be loaded than the asyncio from the standard library. (The folder of the current file always stays before in Python Path). So when you do import asyncio is making a reference to the module itself that is writing (and that does not have the function get_event_loop.

Try simply renaming your file to asyncio_example.py, for example.

(Upon reading the question, I suspected that this was the problem and would put the suggestion as a comment - but you confirmed in the comments that in fact your module has the name of asyncio.py)

  • as a tip to part: if you realemtne will use Windows to develop, I suggest you configure the file browser to display known file extensions. It is very important to know if the file you are viewing is ". py", "pyc", or ". Pyo"m "py~", etc...

  • Your tip solved the problem. I renamed the file and ran without error. Thank you.

  • 1

    If that’s the answer, remember to mark it as correct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.