Error with the asyncio module

2

I'm trying to run the example below on IDLE (Python 3.43, Windows 7, 64bit) for days, but it always gives an error. I tried searching for the solution on the English version website, on google, and I did not find an answer. I can run on Anaconda, but IDLE 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'
    
asked by anonymous 06.07.2015 / 22:47

1 answer

2

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

Just try renaming your file to asyncio_example.py , for example.

(While reading the question, I suspected that this was the problem and would put the suggestion as a comment - but you have confirmed in the comments that your module is actually named asyncio.py )

    
07.07.2015 / 06:27