Reputation: 1
I am trying to import fore into my python code so that I can have colored text but my text doesn't get colored and instead Fore "prints" a string in the cmd prompt that says '<-[37m'
Please help, thanks.
import random, string, requests, time, webbrowser
from colorama import Fore
import concurrent.futures
import os
Title = "poop checker"
print(Title)
print("made by gtrhty| github.com/jfgre4oigjh")
time.sleep(.5)
K4 = int(input(Fore.WHITE+"""
[1] poop
[2] pee
[3] my website
choose one: """+Fore.WHITE))
Upvotes: 0
Views: 181
Reputation: 1
Why is the code doing that?
You need a terminal to support ANSI codes like CMD or PowerShell. If your terminal does not support ANSI codes, switch or enable them.
Non-Supported ANSI Terminal Example:
This is the example on the CMD Python Console which does not support it as noticed:
C:\Users\User>python
Python 3.11.5 (tags/v3.11.5:cce6ba9, Aug 24 2023, 14:38:34) [MSC v.1936 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from colorama import Fore
>>> print(Fore.RED + "ANSI is not supported")
←[31mANSI is not supported
What to do?
If this happens for you, please enable ANSI colors, in some cases, you may need to change the colorama code like this example:
Examples of code and their effect:
from colorama import Fore, init
init()
print(Fore.RED, "red text")
After execution, it works in PowerShell and CMD. They support ANSI color codes and the code works, but some examples may be incorrect like the example on your code, without the init(), it may not work, and here is the code and proof:
from colorama import Fore
print(Fore.RED, "red text")
Here is what happens when that is executed on PowerShell:
PS C:\Users\User> & C:/Users/User/AppData/Local/Programs/Python/Python311/python.exe "c:/Users/User/from colorama import Fore.py"
←[31m red text
Here is another result on CMD:
C:\Users\User>python "c:/Users/User/from colorama import Fore.py"
←[31m red text
These results are different in the Visual Studio Code terminal since they are enabled by default for a better experience.
Guide on fixing the issue:
1. ANSI Color Support
If your terminal doesn't show the colors and it supports ANSI colors. They aren't enabled. This guide is for CMD and PowerShell and it makes a system wide default for the console host on Windows, but, some applications may override this setting.
Press Win + R
, type regedit
and press Enter
or type regedit
into the search bar.
When regedit
is opened, navigate to this path: HKEY_CURRENT_USER\\Console
.
After doing that, make a DWORD (32-bit) Value
inside the Console
key which can done via right clicking the Console
key, select New
and DWORD (32-bit) Value
or just simply making a DWORD (32-bit) Value
inside the Console
key.
Name the DWORD that you made VirtualTerminalLevel
.
After that, modify the Value
to 1 by just double clicking the name and changing the Value
data to 1 or right clicking the name, pressing Modify..., set the Value
data to 1.
When changed, to save the progress, click the OK
button and close the Registry Editor.
When the steps are done, open PowerShell or CMD, run your script and it should be fine.
Tip:
When the program is ran, you notice that the ANSI color also came to your terminal. Use the command for your terminal specified below to fix it:
CMD Command:
color
PowerShell has no alternative code to change the color, just close it and reopen it.
You can also use this to turn off the color changes after the print, this is recommended:
init(autoreset=True)
This is a example of it being used:
from colorama import Fore, init
init(autoreset=True)
print(Fore.RED + "test")
This is what happens when that code is ran in CMD:
C:\Users\User>python "c:/Users/User/from colorama import Fore.py"
test
C:\Users\User>
The color on the code gets a reset, this is good for user experience and in general since when the code is done, the user can keep using the CMD in the normal color.
2. colorama Code
This is just for confirmation, but the first tutorial must be used since it made my terminal work too. On your code, use init like this:
from colorama import Fore, init
init()
print(Fore.RED, "red")
If you only need ANSI color escapes to work, use just_fix_windows_console()
, there is no guarantee it works:
from colorama import Fore, just_fix_windows_console
just_fix_windows_console()
print(Fore.RED, "red")
After doing the first trick, your code runs like this in normal CMD and normal PowerShell:
C:\Users\User>python "c:/Users/User/from colorama import Fore.py"
poop checker
made by gtrhty| github.com/jfgre4oigjh
[1] poop
[2] pee
[3] my website
choose one:
The tags are removed!
The documentation is here talking about just_fix_windows_console()
and also init()
.
This is how to fix ANSI, but this code still has a few things that need a change:
Now, that the ANSI colors are fixed, the code needs changes.
Code Changes
1. First of all, the last line has a slight design flaw:
K4 = int(input(Fore.WHITE+"""
[1] poop
[2] pee
[3] my website
choose one: """+Fore.WHITE))
The last Fore.WHITE
is not needed. The string will be the first defined Fore
, this can be proved by changing the last Fore
color to GREEN which does nothing. Instead remove it. You are using only one string, one Fore
is needed.
2. There are unused modules, I know they will be used soon and they cause nothing different, but, in my opinion, they are not needed.
3. You put most modules seperated by commas, why not put os
and concurrent.futures
? (not the colorama
one)
What I think should be done?
1. Make the ANSI work via the first guide.
2. Implement better coding.
If nothing worked, try installing projects like ANSICON or ConEmu to support ANSI support sequences. However, on newer OSes, the first tutorial should work instantly.
Upvotes: 0