NameError: name 'asyncio' is not defined; In running discord bot
Asked Answered
N

3

5

I'm working on a small discord bot for a server I made, however I've run into a problem. Whenever I try to run the bot (using python3.4 lilac.py), I get the following error:

Traceback (most recent call last):
  File "lilac.py", line 7, in <module>
    @asyncio.coroutine
NameError: name 'asyncio' is not defined
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0xb61ad470>

I tried to install it via python3.4 -m pip install asyncio, and even though it said the install was successful, I still get the error. This is the full code:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='&&', description="Description     here")

@bot.event
@asyncio.coroutine
def on_ready():
    print('Logged in as')
    print(bot.user.name)

Is there anything I didn't install correctly? Am I calling asyncio incorrectly? Any help is appreciated, I can give more info if needed.

Numbing answered 26/8, 2017 at 4:51 Comment(1)
There doesn't seem to be a reasonable reason to down-vote this question (meta.https://mcmap.net/q/212115/-firefox-add-on-tools-for-checking-javascript-syntax/75103). It contains a concise problem description, a small self-contained code sample that reproduces the error, a full traceback, and is sensibly tagged. IOW, it's close to the gold standard for how a question should be presented.Tabby
M
28

You forgot to import asyncio in your code. just add the following into the first line of your code

import asyncio
Midwifery answered 26/8, 2017 at 4:54 Comment(2)
Lord, I'm an idiot. I've been working on stuff for the past 12 hours and I guess I was just in a rush to get this done. Thank you for responding (I'll mark this as accepted once 5 minutes is up, since it won't let me atm)!Numbing
Welcome. happy to helpMidwifery
T
1

You forgot to import asyncio - add the import before you're using it and it should work.

Tepic answered 26/8, 2017 at 4:54 Comment(0)
F
-1

Same problem, check code below

from fastapi import FastAPI
import uvicorn
import asyncio
app = FastAPI()

@app.get("/")

def root():
    return {"message":"Hello World"}

if __name__ == "__main__":
    nest_ayncio.apply()
    uvicorn.run(app)
Fingernail answered 28/5 at 5:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.