Answer the question
In order to leave comments, you need to log in
How to fix NameError: name 'vk' is not defined error in VK bot?
import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
token = 'some token'
vk_session = vk.api.VKApi(token = 'some token')
session_api = vk_session.get_api()
longpoll = VkLongPoll(vk_session)
def sender (id, text):
vk_session.method('messages.send', {'user_id' : id, 'message' : text, 'random_id' : 0})
for event in longpoll.listen():
if event.type = = VkEventType.MESSAGE_NEW:
if event.to_me:
msg = event.text.lower()
id = event.user_id
if msg == 'hello':
sender(id, 'hi!')
I'm making a VK bot and it gives me an error:
C:\Users\USER>"C:\Users\USER\Desktop\SERVER_VK\main.py"
Traceback (most recent call last):
File "C:\Users\USER\Desktop\SERVER_VK\main.py", line 6, in
vk_session = Vk.api.VKApi(token = 'some token')
NameError: name 'Vk' is not defined
Answer the question
In order to leave comments, you need to log in
Everything is written correctly, the vk variable is not defined.
vk_session = vk.api.VKApi(token = 'какой-то токен')
vk_session = vk_api.VKApi(token = 'какой-то токен')
Incorrect line vk_session = Vk.api.VKApi(token = 'some token')
Correct: vk_session = vk.api.VkApi(token = 'some token')
Ps If it helped, let me know
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question