0

I am currently designing a simple game and I am trying to change the colour of specific things I am printing. Already I have downloaded the colorama module and ran my code using a sample I've found online, but this doesn't work (I'll tell the details later). I am aware of a setting within the Python options but this affects the whole text not specific sections. If this helps my computer runs Windows and I am running Python 3.8. Here is the setup code, and then a few lines after is the code I have in a separate file. A few lines after that is the output:

from setuptools import setup, find_packages

name = 'colorama'
version = '0.1'

def get_long_description(filename):
    readme = join(dirname(__file__), filename)
    return open(readme).read()


setup(
    name=name,
    version=version,
    description="Cross-platform colored terminal text.",
    long_description=get_long_description('README.txt'),
    keywords='color colour terminal text ansi windows crossplatform xplatform',
    author='Jonathan Hartley',
    author_email='tartley@tartley.com',
    url='http://code.google.com/p/colorama/',
    license='BSD',
    packages=find_packages(exclude=['ez_setup', 'examples', 'tests']),
    include_package_data=True,
    zip_safe=True,
    install_requires=[
      # -*- Extra requirements: -*-
    ],
    entry_points="""
# -*- Entry points: -*-
    """,
    classifiers=[
        'Development Status :: 2 - Pre-Alpha',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'License :: OSI Approved :: BSD License',
        'Operating System :: OS Independent',
        'Programming Language :: Python :: 2.6',
        'Topic :: Terminals',
    ]
    # see classifiers http://pypi.python.org/pypi?%3Aaction=list_classifiers
)



import colorama
from colorama import Fore, Style
print(Fore.BLUE + "Hello World")



[34mHello World

2 Answers2

0

The documentation states that you need to start by calling the init() function

Applications should initialize Colorama using:

   from colorama import init 
   init()

which has the following effect

On Windows, calling init() will filter ANSI escape sequences out of any text sent to stdout or stderr, and replace them with equivalent Win32 calls.

which is exactly what you see it's happening to you because you see the [34m in the beginning.

Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
0

you need to call init() somewhere after you import colorama and before you print a string with a color. for example:

import colorama
from colorama import Fore, Style
init()
print(Fore.BLUE + "Hello World")
Dharman
  • 30,962
  • 25
  • 85
  • 135
andypaling1
  • 142
  • 12