How to extract everything from the file without error. Here is an example.

def __code_import__(): from colorama import init, Fore, Back, Style init() #DEFAULT_COLOR FW = Fore.WHITE FG = Fore.GREEN FC = Fore.CYAN FB = Fore.BLUE FR = Fore.RED FM = Fore.MAGENTA FY = Fore.YELLOW FBB = Fore.BLACK #LIGHT_COLOR FLW = Fore.LIGHTWHITE_EX FLG = Fore.LIGHTGREEN_EX FLC = Fore.LIGHTCYAN_EX FLB = Fore.LIGHTBLUE_EX FLR = Fore.LIGHTRED_EX FLM = Fore.LIGHTMAGENTA_EX FLY = Fore.LIGHTYELLOW_EX FLBB = Fore.LIGHTBLACK_EX #BACKGROUND_DEFAULT_COLOR BW = Back.WHITE BG = Back.GREEN BC = Back.CYAN BB = Back.BLUE BR = Back.RED BM = Back.MAGENTA BY = Back.YELLOW BBL = Back.BLACK #BACKGROUND_LIGHT_COLOR BLW = Back.LIGHTWHITE_EX BLG = Back.LIGHTGREEN_EX BLC = Back.LIGHTCYAN_EX BLB = Back.LIGHTBLUE_EX BLR = Back.LIGHTRED_EX BLM = Back.LIGHTMAGENTA_EX BLY = Back.LIGHTYELLOW_EX BLBL = Back.LIGHTBLACK_EX #STYLE SRA = Style.RESET_ALL SD = Style.DIM SB = Style.BRIGHT 

When I extract a simple import, an error occurs that the name FLC could not be determined!

  • why do you think local variables should be accessible through "simple import". Give obviously a minimal example of code that you expected to work. - jfs

1 answer 1

This variable is available only inside the "__code_import__" function.

Not sure if this is correct, but I would do so.

 # your_module source from colorama import init, Fore, Back, Style FW = Fore.WHITE # ------- Other colors -------- SB = Style.BRIGHT def __import__(): init() # your program source from your_module import * print(FLC) 

UPD And even better through the class:

 class MyColors(object): from colorama import Fore, Back, Style self.FW = Fore.WHITE # ------- Other colors -------- self.SB = Style.BRIGHT def __import__(): from colorama import init init() 

And call, like this:

 from my_module import MyColors print(MyColors.SB + "string with color")