1

I use pprint.pprint for every debug output I ever make, across every python file I've written is the line from pprint import pprint.

Often times once I have a stable version of that file I forget to drop the import even if all pprint statements are removed.

I would like to have pprint available in all context, all-ways on my development machine so my testing machine will fail when debug output has been left in and so I don't have to spend an alarmingly large portion of my life typing that one line.

How do I pre-import this tool system wide?

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
  • Probably a duplicate of: http://stackoverflow.com/questions/11124578/automatically-import-modules-when-entering-the-python-or-ipython-interpreter – clockwatcher Jan 29 '15 at 07:06
  • @clockwatcher I could be wrong, but that seems specific to the interactive interpreter. – grc Jan 29 '15 at 07:07
  • An aside: learning to use the [`logging`](https://docs.python.org/2/library/logging.html) module will really save you a lot of time and headaches – Adam Smith Jan 29 '15 at 07:08
  • 2
    You might want to consider whether you really _want_ to do this. What if you accidentally forget to remove the `pprint` statement when you push your development code to production? All of a sudden, things start to fail with `NameError`... Oops... – mgilson Jan 29 '15 at 07:17
  • @mgilson That is more or less desirable, pprint is used for debugging and its sister call pformat would be used in production, I am comfortable doing a `print(pprint.pformat(...))` as a work around/quirk of this workflow. – ThorSummoner Jan 29 '15 at 07:50
  • @ThorSummoner -- So you want your code to start failing accidentally when you push it to production? ;-) I think a better solution is to get a good linter which will yell at you when you have an unused import. That way, once you're done debugging and all your pprint statements are removed, the liter will let you know that you should also remove the import ... But that's just my 2c. – mgilson Jan 29 '15 at 07:56
  • @mgilson That's very misunderstanding of the whole idea. I mention in the post that this would be applied to a development machine, which under my practices would never deploy to production without being tested in an identical environment. I already use linters that tell me I have unused imports, what I don't have is an automatic knock on the head anytime I leave debug output in my commits, or the spare time to write the same lengthy import statement once per every file I ever write. Do what you like though, matter of course. – ThorSummoner Jan 29 '15 at 08:36

1 Answers1

1

As per this answer to a similar question, you could modify /lib/site.py for every different version of Python you have on the system. Note that I highly recommend that you DON'T do this, but if necessary then you could. You should modify it in this way:

import sys
import os
import builtins
import _sitebuiltins
import pprint # <---------------------------- added

...

def main()
    """Add standard site-specific directories to the module search path.

    This function is called automatically when this module is imported,
    unless the python interpreter was started with the -S flag.
    """
    global ENABLE_USER_SITE

    builtins.pprint = pprint.pprint # <------ added

    ...

Beware that this can cause some really funky bugs with production code that uses pprint even though it's never been imported.

Community
  • 1
  • 1
Adam Smith
  • 52,157
  • 12
  • 73
  • 112