equivalent of raw_input in Ipython notebook

15,155

Solution 1

IPython 2 now supports "raw_input", IPython 3 supports "input". Notice that "input" is present in IPython 2, but it is not the same as in IPython 3! Rather it does the equivalent of eval(input( )).

(this is not a particularity of IPython, it is just behaviour inherited from Python 2/3)

If you want something portable in a notebook, just write towards the beginning of it:

try:
    input = raw_input
except NameError: #Python 3
    pass

... and then always use input.

Solution 2

raw_input will work in the notebook in IPython 1.0, pending this pull request.

Share:
15,155

Related videos on Youtube

Ryan Saxe
Author by

Ryan Saxe

Updated on September 15, 2022

Comments

  • Ryan Saxe
    Ryan Saxe over 1 year

    I am just messing around with Ipython notebook, and I was going to create a battleship game...unfortunately I need lines like the following in the code in order to make a game like that:

    move = raw_input("Where would you like to attack? ")
    

    Ipython notebook does not allow raw_input...so how could I get input from one of the players? I have searched around and nothing I could find had a direct answer to this such as no you can't or yes and this is how. Thanks.

  • Eric Brown
    Eric Brown almost 10 years
    Can you add a bit of explanation as to why this works?
  • drevicko
    drevicko almost 10 years
    As far as I'm aware, the tk dialog will appear on the computer running the ipython server, and then only if the session that launched it is associated with a screen. If you're looking at the notebook remotely, you won't see the dialog..
  • dashesy
    dashesy about 6 years
    So IOW, always use raw_input instead of input?
  • Pietro Battiston
    Pietro Battiston about 6 years
    @dashesy No! Rather use input (see the last line I added above), and use the snippet when you need backward compatbility with python 2.
  • dashesy
    dashesy about 6 years
    yes that makes more sense. In ipython (Python2) input does the wrong thing (of course unless we do input=raw_input`, which confused me.