Search code examples
pythonpython-3.xlinuxoperating-systemsublimetext3

os.system('clear') in code returns "TERM environment variable not set" instead of clearing console


On linux, using sublime text. os.system('clear') should just clear the console window. os.system('cls') for windows. But it's returning different from me.

code:

import os

clear = lambda: os.system('clear')

move_dict = {'rock': 1, 'paper': 2, 'scissors': 3}

user1 = input('Rock, paper or scissors: ').lower().strip()
user1_x = move_dict.get(user1)
clear()
user2 = input('Rock, paper or scissors: ').lower().strip()
user2_x = move_dict.get(user2)
clear()


diff = user1_x - user2_x


if diff in [-1, 2]:
    print(f'Player 1\'s {user1} loses. Player 2\'s {user2} wins.')
elif diff in [1, -2]:
    print(f'Player 1\'s {user1} wins. Player 2\'s {user2} loses.')
else:
    print(f'Both players chose {user1}, draw.')

returns:

Rock, paper or scissors: rock
TERM environment variable not set.
Rock, paper or scissors: scissors
TERM environment variable not set.
Player 1's rock wins. Player 2's scissors wins.

why is "TERM environment variable not set" returning when clear runs instead of just clearing the console?

tried defining the function using def instead of lambda, same error. didn't help. Previously asked similar questions don't lay out beginner friendly answers.


Solution

  • interesting, os.system('clear') works for me totally fine. I had to os.system('unset TERM; clear') to reproduce your error.

    Most likely your TERM env is somewhere unset, uninherited when you get to your python script

    the simplest way is to os.system('TERM=xterm-256color clear') you can replace xterm-256color with whatever is supported by your terminal