How does python differ when launched interactively?

I have some Python code that works as expected if I type commands once in a while using Python interactive mode. The same code crashes if it is saved as myscript.py and works as 'C:\Python27\python.exe myscript.py' .

How does running Python code as a script result in a crash if the same code runs interactively?

This question asks how to find out if python is online. However, the appellator needs only one reliable fingerprint of the interactive mode. I need a list of ways in which interactive mode is different, with particular attention to the problems that this may cause.

For instance:

  • sys.path may vary
  • os.getcwd() may vary
  • os.environ may vary
  • All answers to this question.
  • This is a warning at the beginning of the multiprocessing module documentation.

What else could be between Python interactive and script mode?

+4
source share
3 answers

It looks like you are interacting with hardware that reveals the most striking difference between REPL and script:

Commands in the script are executed as soon as possible, while the REPL is waiting for user input.

That is, you probably have a synchronization problem when the hardware is not ready for the next command so quickly after the previous one.

+1
source

Thread and Greenlet have different behavior in an interactive environment. In some cases, the main event loop needs to be cracked.

Greenlet is a gevent module that is a parallel task in python. It has an internal context switch other than Python (pthread), and concurrency works very well (in my experience). Some of the problems with Greenlets are that they block the blocking of C-system calls and socket interactions if they are not patched by monkeys (module in gevent ).

The main loop of the loop must be fixed so that the green panels work correctly ... If you create a green shell in an interactive environment, it will not switch contexts and will not execute, I forgot, from my point of view, how to fix the main loop of the event (it will be added later).

Failure example:

 In [1]: from gevent.greenlet import Greenlet In [2]: def print_hi(): ...: print 'hi' ...: In [3]: print_hi() hi In [4]: g = Greenlet(print_hi) In [5]: g.start() 

Edit:

After looking at some code for this project here, we cracked the ipython input hook to use gevent

 import sys import select import gevent def stdin_ready(): infds, outfds, erfds = select.select([sys.stdin], [], [], 0) if infds: return True else: return False def inputhook_gevent(): try: while not stdin_ready(): gevent.sleep(0.001) except KeyboardInterrupt: pass return 0 # install the gevent inputhook from IPython.lib.inputhook import inputhook_manager inputhook_manager.set_inputhook(inputhook_gevent) inputhook_manager._current_gui = 'gevent' # First import the embeddable shell class from IPython.frontend.terminal.embed import InteractiveShellEmbed 

Corrected example:

 In [6]: def say_hi(): ...: print "hi" ...: In [7]: g = gevent.greenlet.Greenlet(say_hi) In [8]: g.start() In [9]: hi <-- Cursor is here so it printed hi 
+3
source

Look at this line

 PCO_api = ctypes.oledll.LoadLibrary("SC2_Cam") 

If you can use COMPLETE path for LoadLibrary

0
source

Source: https://habr.com/ru/post/1412075/


All Articles