I want to add a debug print statement test, if I enable --verbose from the command line and if I have the following in the script.
logger.info(\
if you want to enable logging.DEBUG level for a script you don't want to (or cannot) edit, you can customize your startup:
jcomeau@aspire:~$ python -c "import site; site._script()"
[snip]...
USER_BASE: '/home/jcomeau/.local' (exists)
USER_SITE: '/home/jcomeau/.local/lib/python2.7/site-packages' (exists)
ENABLE_USER_SITE: True
jcomeau@aspire:~$ mkdir -p ~/.local/lib/python2.7/site-packages
jcomeau@aspire:~$ vi ~/.local/lib/python2.7/site-packages/usercustomize.py
enter the following:
import os, logging
if os.getenv('DEBUGGING'):
logging.basicConfig(level = logging.DEBUG)
then you can just:
jcomeau@aspire:~$ mkdir -p /tmp/some/random/
jcomeau@aspire:~$ echo 'import logging; logging.debug("test")' >> /tmp/some/random/script.py
jcomeau@aspire:~$ DEBUGGING=1 python /tmp/some/random/script.py
DEBUG:root:test
from Paul Ollis at http://nedbatchelder.com/blog/201001/running_code_at_python_startup.html
2017-07-18: I've since switched to a different method:
logging.basicConfig(level=logging.DEBUG if __debug__ else logging.INFO)
what this does is, if you're running without optimization (as in python script.py) you get the DEBUG-level stuff, whereas if you run with python -OO script.py you don't. no environment variables to set.