Search code examples
pythonoperating-systemsubprocesssystelebot

local variable 'subprocess' referenced before assignment


Here is it my code, I'm trying to perform command in commandline and get output, but I got error. Please help me

import telebot
import subprocess


token = ''
bot = telebot.TeleBot(token)

@bot.message_handler(commands=['echo'])
def changeWall(message, res=True):
    subprocess = subprocess.Popen("echo Hello World", shell=True, stdout=subprocess.PIPE)
    subprocess_return = subprocess.stdout.read()
    print(subprocess_return)
    bot.reply_to(message, subprocess_return)

Error:

local variable 'subprocess' referenced before assignment

Solution

  • You need to change the name of the subprocess variable.

    You can read more here: Python: function and variable with the same name

    Also I recommend that you remove the token from the code.