Search code examples
pythoninteractiveinteractive-mode

In what ways is python different when run in interactive mode?


I have some Python code that works as expected if I type the commands one-at-a-time using Python's interactive mode. The same code crashes if saved as myscript.py and run as 'C:\Python27\python.exe myscript.py'.

In what ways could running Python code as a script cause it to crash, if the same code works in interactive mode?

This question asks for ways to tell if python is in interactive mode. However, the asker just wants a single, reliable fingerprint of interactive mode. I'd like a list of ways that interactive mode is different, with particular attention to problems this can cause.

For example:

  • sys.path could be different
  • os.getcwd() could be different
  • os.environ could be different
  • All the answers to this question
  • This warning at the beginning of the multiprocessing module documentation

What else could be different between Python's interactive and script mode?


Solution

  • It looks like you're interacting with hardware, which brings up the most glaring difference between the REPL and a script:

    Commands in a script run immediately as soon as possible, whereas the REPL waits for human input.

    That is to say, you are probably having a timing issue where the hardware isn't ready for the next command so soon after executing the previous.