I want to change how IPython handles default import errors. When I prototype something in the IPython shell, I usually forget to import os
, re
or whatever I need first. The first few statements often follow this pattern:
In [1]: os.path.exists("~/myfile.txt") --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-0ffb6014a804> in <module>() ----> 1 os.path.exists("~/myfile.txt") NameError: name 'os' is not defined In [2]: import os In [3]: os.path.exists("~/myfile.txt") Out[3]: False
Of course, my fault is for bad habits and, of course, in a script or a real program that makes sense, but in the shell I would prefer IPython DWIM, at least trying to import what I'm trying to use.
In [1]: os.path.exists("~/myfile.txt") --------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-1-0ffb6014a804> in <module>() ----> 1 os.path.exists("~/myfile.txt") NameError: name 'os' is not defined Catching this for you and trying to import "os" … success! Retrying … --------------------------------------------------------------------------- Out[1]: False
If this is not possible with iphone vanilla, what do I need to do to make this work? Is the wrapping core the easiest way forward? Or should it be implemented directly in the kernel using a magic command?
Please note that this is different from such a question where someone wants to always load predefined modules. I do not. Because I do not know what I will work on, and I do not want to download everything (and I do not want the list of all to be updated.
source share