You Can Detect if Code Is Being Run Inside a Terminal 0 ▲ Nelson Figueroa 1 day ago · Tech · hide · 0 comments I learned about the sys.stdout.isatty() method in Python which allows you to detect if your code is running in a terminal. For example: Pythonimport sys if sys.stdout.isatty(): print("This program is running in a terminal!") else: print("This program is running somewhere else, like a CI/CD pipeline")Running the program outputs the following: Console$ python3 tty.py This program is running in a terminal! But if we pipe the program into some other tool, like cat, it is no longer running in a terminal and prints the other message: Console$ python3 tty.py | cat This program is running somewhere else, like a CI/CD pipeline This is why some colorized output from code is not colorized in CI/CD pipelines. The code itself checks if it’s being run in a terminal, and if not, it doesn’t display colors that would result in junk being displayed due to the nature of ANSI escape codes used in colorization. I always thought that it was the CI/CD systems that suppressed color output but that’s not… No comments yet. Log in to reply on the Fediverse. Comments will appear here.