Reputation: 70
When I try to call a function from another python code file, I get this error:
Traceback (most recent call last):
File "%filepath%", line 2, in module
print(colour(red) + "Test")
NameError: name 'red' is not defined
from this code:
from hcolours import *
print(colours(red) + "Test")
When I have it defined in my code:
def colours():
reset = '\033[0m'
bold='\033[01m'
disable='\033[02m'
underline='\033[04m'
reverse='\033[07m'
strikethrough='\033[09m'
invisible='\033[08m'
black='\033[30m'
red = '\033[31m'
and so on (the full code is here)
I have tried lots of different ways to get it to work but I can't understand it, I have even tried putting it up on PyPI
Do you need to do something special?
I'm completely stuck
Upvotes: 0
Views: 110
Reputation: 2012
Seems you mix function
and class
...
From your code, you want to print with color, so you defined a function, but without argument. Maybe you want a class here?
class colours:
reset = '\033[0m'
bold='\033[01m'
disable='\033[02m'
underline='\033[04m'
reverse='\033[07m'
strikethrough='\033[09m'
invisible='\033[08m'
black='\033[30m'
red = '\033[31m'
print(colours.red + "Test" + colours.reset)
Then a possbile function could be
class colours:
reset = '\033[0m'
bold='\033[01m'
disable='\033[02m'
underline='\033[04m'
reverse='\033[07m'
strikethrough='\033[09m'
invisible='\033[08m'
black='\033[30m'
red = '\033[31m'
def print_colours(str):
print(colours.red + str + colours.reset)
#we can test the string as below
#print_colours("Test")
Upvotes: 1