1

I am writing a program and I need to detect whether a function is being called inside the shell or inside a file:

Image for clarification

Is this possible? maybe with idlelib or something?

Cheers!
StupidWolf
  • 45,075
  • 17
  • 40
  • 72
Leo
  • 13
  • 6

1 Answers1

1

Origial: Duplicate question. if 'idlelib.run' in sys.modules: may be the best answer. Add and sys.stdin is not sys.__stdin__ to be even surer.

Edit: this is the correct answer to this question: how can I tell whether my code was run in IDLE or directly in Python, without IDLE. The person who closed this question thought the same thing, as the supposed duplicate answered this question.

Running code from an IDLE editor runs the code essentially the same as if it were entered in the shell. That is why print output appears in Shell, followed by a shell prompt. So without being able to verify for myself, I will not believe that identical code ran 10x faster when run from the IDLE editor.

Original: Printing lots of shorts strings is slower in the shell because of the overhead of interprocess communication. Joining strings before printing is faster because less overhead per char.

Edit: Here I was comparing running in IDLE versus running in Python without IDLE. I have seen printing speed differences of 10x in those two very different situations.

Original: If something else, ask a new question and I will look at it.

Edit: Above I said 'run from shell prompt' and 'run from editor' were essentially the same. There is a slight different between 'Shell started normally, without running a file first' and 'Shell started by first running a file from the editor'. It is the presence of attribute file in module main, which has to be imported.

================================ RESTART: Shell ====================
>>> import __main__
>>> hasattr(__main__, '__file__')
False
>>> 
========================= RESTART: F:\Python\a\tem-3.py ===
# File has same two lines above.
True
# But note that subsequent calls entered here now return True.
>>> hasattr(__main__, '__file__')
True
>>> 

EDIT Summary: a function run in IDLE's Shell can tell how Shell was last started or restarted. As near as I can tell, it cannot tell how it was entered. In either case, the function is compiled the same and has the same attributes. So it seems reasonable that it can only detect differences in the environment caused by the different in how the environment was set up. And the only environmental difference I could find was the presence or not of main.file when started with a file or not.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
  • Hmm... it doesn't seem to work... I'll try and look at the site that was reccommended – Leo Jun 20 '21 at 08:47
  • What does not work? On what system and version? I just retried both conditionals. The second could be true even if the first is not, so the first is necessary. – Terry Jan Reedy Jun 21 '21 at 04:29
  • I tried it in the shell and the files: [img](https://i.stack.imgur.com/iugpm.png) – Leo Jun 22 '21 at 15:00