1

I am enjoying developing inside the ipython notebook, but I am having a problem when I want to write a main() function that reads the command line args (with OptionParser, for example). I want to be able to export the code to a .py file and run it from the command line, but I haven't found a way to have a main() that runs both in the notebook with predefined arguments or from the command line with python and command line args. What is the secret?

In case that is not clear, I would like to do something like this:

if __name__ == '__main__':
    # if in the notebook
    vals = {'debug':True, 'tag_file': 't.tags'}
    options = Object()
    for k,v in vals.items():
        options.setattr(k,v)
    args = 'fname1.txt'
    # if running as a command line python script
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option('-d','--debug',action='store_true',dest='debug')
    parser.add_option('-t','--tags',action='store',dest='tag_file')
    options,args = parser.parse_args()
user1427057
  • 171
  • 1
  • 9

2 Answers2

1

You cannot determine that you are in an IPython notebook or a qtconsole, or a simple IPython shell, for the simple reason the 3 can be connected to the same kernel at the same time.

It would be like asking, what color is the current key the user is typing. You could get it by looking the plugged usb devices and look for images on the internet and guess the keyboard color, but nothing guarantees you it will be accurate, nor that it won't change, and user can have multiple keyboard plugged, or even painted keyboard.

It is really the same with the notebook, Even if you determine you are in ZMQKernel, are you speeking to qtconsole or webserver ? Again, you found that you were talking to the webserver, are you talking to JS or Emacs ? And so on and so forth.

The only thing you can do, you can ask the user.

What is reliable, is test wether you are in IPython or not.


If you really but reeaaalllyy want a way, as until now, the notebook is the only thing that can display Javascript. And javascript can execute code in pyton. So you might be able to create something that display JS that send back info to the kernel. And using thread and timer you can say that you were not in a notebook (but you will have a race condition).

Matt
  • 27,170
  • 6
  • 80
  • 74
  • I am ok with that. How do I test if I am in IPython or not? When running at the command line, I will be using python, not ipython. – user1427057 Jun 03 '13 at 21:17
  • Thanks, given this hint, I found the answer by writing a in_ipython() function (from the stackoverflow post). I repost the code here: inline `def in_ipython(): try: __IPYTHON__ except NameError: return False else: return True` – user1427057 Jun 03 '13 at 22:13
0

Don't worry about the distinction. Just set default values, and unless they are overridden from the command line, use those.

if __name__ == '__main__':

    parser = OptionParser()
    parser.add_option('-d', '--debug', action='store_true', dest='debug',
                      default=True)
    parser.add_option('-t','--tags',action='store',dest='tag_file',
                      default='t.tags')
    options, args = parser.parse_args()
    if not args:
        args = ['fname1.txt']
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 1
    parser.parse_args() fails when run from inside the ipython notebook with a SystemExit: 2 and a "Usage: -c [options]" message. That is why I posted this question. – user1427057 Jun 03 '13 at 22:11
  • What happens if you pass an explicit empty list to `parse_args()`? I'm not familiar with the `ipython` notebook, but perhaps there is just an issue with accessing `sys.argv`. If that makes no difference, I'll delete this answer. – chepner Jun 03 '13 at 23:01