How to log everything that occurs in a Python interactive shell session?

后端 未结 5 1512
小蘑菇
小蘑菇 2021-01-05 09:23

I\'d like to have real-time access to both interpreter input and error and standard output. Preferably this information would be written to a file, so that I can poll the fi

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-05 10:03

    See this Virtualenv article by Doug Hellmann, showing how to log an iPython session:

    If you are comfortable working at the interactive prompt in this way, but want to record what you do for future reference after you close your session, you can use IPython’s logging feature to write the session to a file. To activate the log, use the control command %logstart, as illustrated in Listing 5. The output file is a Python source file, so it is easy to clean it up and turn it into a “real” module when you are done experimenting.

    In [6]: %logstart
    Activating auto-logging. Current session state plus future input saved.
    Filename       : ipython_log.py
    Mode           : rotate
    Output logging : False
    Raw input log  : False
    Timestamping   : False
    State          : active
    
    In [7]: a = 5
    
    In [8]: b = 6
    
    In [9]: c = a * b
    
    In [10]: c
    
    Out[10]: 30
    
    In [11]: d = [ a, b, c]
    
    In [12]: d
    
    Out[12]: [5, 6, 30]
    
    In [13]: %logstop
    

提交回复
热议问题