How to detect if a Python script is executed from inside IDEA/PyCharm ?

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?

6 comments
Comment actions Permalink

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.

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-consol

e-does-support-ansi-escape-codes-in-python

How do I tune the code in order to use ANSI coloring when it is

available?

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

2
Comment actions Permalink

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?

1
Comment actions Permalink

Hello Sorin,

The only other solution would be for PyCharm to define such an environment

variable automatically in all run configurations.

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?

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

1
Comment actions Permalink

That would be great! Should I add  feature request to the tracker for this?

1
Comment actions Permalink

Hello Sorin,

Please do.

That would be great! Should I add  feature request to the tracker for

this?

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

1
Comment actions Permalink

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 :-)

1

Please sign in to leave a comment.