How To Import Colorama In Python

In this tutorial, we will take you through how to import the Colorama library in Python. The Colorama library allows you to easily add colors and styles to your Python console output. By following the steps outlined below, you’ll be able to create visually appealing console applications in Python.

Step 1: Install Colorama

Before you can import Colorama in your Python script, you need to install it first. To do this, open your terminal or command prompt and type the following command:

This command installs the latest version of Colorama available on PyPI (The Python Package Index). Ensure you have Python and pip installed on your system.

Step 2: Import Colorama

To import Colorama, add the following line of code at the beginning of your Python script:

Now that you have Colorama imported, you can use it to manipulate the style and color of your console outputs.

However, to use Colorama effectively, you’ll need to initialize it using the following line of code:

Setting autoreset=True enables Colorama to automatically reset the console output style after each print statement, so your console colors won’t bleed into subsequent lines.

Step 3: Use Colorama to Style Your Console Output

To use Colorama’s functions in your script, you’ll need to import its submodules for foreground colors (Fore), background colors (Back), and text styles (Style). Add the following lines of code after initializing Colorama:

Now, you can use the different color and style options provided by these submodules to style your console outputs.

This sample code snippet demonstrates how to change the text color, background color, and style when using the Colorama library. You can find a complete list of available colors and styles on the Colorama PyPI page.

Full Code

Output

Conclusion

In this tutorial, you learned how to import and use the Colorama library to style your Python console output. Following these steps, you can easily add colors and text styles to your console applications, making them more visually appealing and user-friendly.