I would like to repeat calls to Python scripts using% run in an ipython session, and for each of these scripts to register based on cmdline arguments passed through% run.
For example, during debugging of cmd.py, over time I want to run:
%run cmd.py ... logs with default behavior, eg to STDERR with root level WARN %run cmd.py
Unfortunately, this is complicated because the logging state created by logging.basicConfig is saved after the first% run command (this usually applies to all modules and is often desirable when using% run).
I understand that in complete generality, a number of% run commands, as mentioned above, will not be the same as starting each command in a new process. However, it would be very convenient if things like log_level and log_file could be reinitialized.
I tried something like this in cmd.py:
import logging_config # parse logging config from sys.argv reload(logging_config) # re-parse cmdline if using %run multiple times
and logging_config.py does (is compressed):
if logging_initialized: logging.getLogger().setLevel(lvl) else: logging.basicConfig(level=lvl) logging_initialized = True
It works for simple cases, but not if cmd.py imports libraries that also use logging. I also experimented with logging.shutdown () (called at the end of each cmd.py), but that doesn't seem to help.
source share