How to detect if a Python script is executed from inside IDEA/PyCharm ? Follow
Recently IDEA/PyCharm added support of ANSI escape codes in the console. Still this does not mean that scripts will always be able to use coloring.
Here is the problem: normally a script is supposed to detect if it is executed from inside a tty or not. The PyCharm console is supposed to be a tty but it seams that it isn't. In fact it seams to be a file and due to this the color output will not work (you are not supposed to output ansi escapes if your stdout or stderr is a file).
Here is the detailed question with sample code http://stackoverflow.com/questions/7445658/how-to-detect-if-the-console-does-support-ansi-escape-codes-in-python
How do I tune the code in order to use ANSI coloring when it is available?
Please sign in to leave a comment.
Hello Sorin,
There is no general solution to this problem. You can define an environment
variable in your run configurations (RUNNING_INSIDE_PYCHARM or something
like that) and check for its existence in your scripts.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Hi Dmitry,
Thanks for the hint but I cannot consider this as solution. I am looking for something that would not require people to do something in order to use this functionality.
Is it impossible to detect PyCharm environment from the running script?
Hello Sorin,
The only other solution would be for PyCharm to define such an environment
variable automatically in all run configurations.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
That would be great! Should I add feature request to the tracker for this?
Hello Sorin,
Please do.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Added as http://youtrack.jetbrains.net/issue/PY-4853 - don''t forget to vote for it if you want to see coloring sooner in PyCharm console :-)