r/IPython • • Feb 28 '15

What exactly is IPython?

Hello,

I am relatively new to learning Python, and am confused by exactly what IPython is. Is it an implementation of Python like CPython, Jython, PyPy et al.? Or is it more like an IDE like Eric or Spyder? Or is it an IDE that is integrated with its own implementation of Python? Would the Notebook be part of that IDE? Is there an ELI5 explanation for how to conceptualise IPython?

Thanks!!

15 Upvotes

20 comments sorted by

8

u/hharison Mar 01 '15 edited Mar 01 '15

IPython is a system for interactively running Python code. It runs a Python kernel in the background and provides several frontends. The console is like the regular Python console in a terminal with some extra features. The IPython QTConsole is similar but in a QT-based graphical frontend which allows for things like copying and pasting that are difficult in a regular terminal. The QTConsole can also display plots.

The flagship product is the IPython Notebook. This lets you intersperse code, text, and plots.

It's not an IDE; it doesn't do any project-management stuff and it's not really intended for making code that you'll distribute in any other format but the notebook. It's mostly used in a scientific context, for example to demonstrate a data analysis technique. You can describe the technique in regular paragraph form, and include the code at the same time, along with the results.

For example: http://nbviewer.ipython.org/github/adrn/ipython/blob/2.x/examples/Notebook/Trapezoid%20Rule.ipynb

Some IDEs, like Spyder and PyCharm, can run the IPython console or qtconsole as the terminal section of the GUI. Think of IPython like that: it's just the part of the IDE (or a development environment of multiple programs) for interactively running code, testing stuff out, etc.

2

u/avamk Mar 01 '15

Ah I see. I saw your answer just after I submitted my reply to /u/PigBoyOhBoy 's answer.

Just to make sure I understand correctly: I guess it running "a Python kernel" in the background means e.g. the stock Python that comes with my OS (e.g. Python 2.7 or 3.4 or whatever) is the "actual" Python that's running, and IPython provides several alternative interactive shells to the stock shell that I would get by entering "python" at the command prompt. And those alternative shells include a text-based console, or a cool QTConsole with useful features. Would this be a correct understanding?

As for being part of an IDE, I guess having an IPython console in it just gives me that much more features for the console than using the stock one?

Thanks!!

3

u/hharison Mar 01 '15

It's worth also mentioning that the basic idea has been expanded on and you can use the interactive shells to interact with backends in different languages, notably Julia and R. The general project is called Jupyter. So this another big advantage of IPython---in one notebook you can run Python code and R code, for example.

2

u/avamk Mar 01 '15

Wow that sounds really cool! Since I know a teeny bit of R here's an important question:

In an IPython notebook, can the Python and R code both act on and use the same data and share stuff between them??? If so wouldn't that be great and you get the best of both worlds?

2

u/hharison Mar 01 '15

Yes, and yes it is awesome. I do it all the time, since I like to do my data-munging in Python but I often am using statistical methods that no one's ported to Python yet.

It all works through rpy2. You can use rpy2 methods directly, and they're also exposed in IPython's "R magic" commands as well as in pandas through some DataFrame methods.

1

u/avamk Mar 01 '15

Woah... R "magic", "pandas" (???), DataFrame methods, wha?? Are they Python libraries or something?

3

u/hharison Mar 02 '15

IPython magic are special commands that you can run in IPython that start with %. These include commands like %R which is colloquially referred to as "R magic". It uses rpy2 under the hood to convert objects and run R code. Check out the IPython docs.

(Methods are functions attached to objects)

Pandas is a python library that provides an object DataFrame which is similar to R's data.frame. There is a method DataFrame.to_r_dataframe (forget the exact name, am on my phone so can't quickly look it up) which converts it to R using rpy2.

So basically there are three ways to do the same thing:

  1. Use rpy2 to convert objects between python and R, and run R code passed as strings.

  2. Use IPython's R magic %R magic to run a line of code in R (there is also %%R in the notebook to run a whole cell in R, and magic to convert objects back and forth).

  3. Use pandas.DataFrame.to_r or whatever to convert a DataFrame to R. (You will need to use one of the above techniques to then operate on it).

Read the rpy2/IPython/pandas docs for details.

1

u/avamk Mar 02 '15

Sounds great, I will definitely check out the documentation as I learn all this stuff.

Thank you for your detailed reponses!

2

u/MeshachBlue Mar 01 '15

Yup, definitely can. You can make an "R cell" with cell magic. Let me know if you want help making this happen.

2

u/[deleted] Mar 01 '15

Yes with rpy2. To send data from R to Python you can use the -o flag per this documentation.

2

u/hharison Mar 01 '15

Yes exactly. Besides the notebook it's a relatively small step up from the regular Python console so just give it a try!

2

u/isforinsects Mar 01 '15

Well, consider ipython and ipython notebook a bit separate first. Ipython is an interactive python shell with extra features beyond the standard python interactive shell. Colors, history, and getting the docstring of a function by typing function? are examples of ipython's improvements.

Ipython isn't a production environment for running standard non-interactive python (generally, there are exceptions to this rule).

1

u/avamk Mar 01 '15

Ipython isn't a production environment for running standard non-interactive python

Since I am a super beginner learning Python, what does this mean for mean? Would I still be able to follow, say, a textbook's examples, create Python *.py scripts, and run them in IPython? And can one use IPython for Python application development? Or is IPython used in a different way?

2

u/hharison Mar 01 '15

Since I am a super beginner learning Python, what does this mean for mean? Would I still be able to follow, say, a textbook's examples, create Python *.py scripts, and run them in IPython?

Yes, if you run ipython script.py it will have pretty much the same result as python script.py. There may be an advantage of doing so but I don't know exactly what it is. The use case is more of doing things interactively than running scripts.

And can one use IPython for Python application development? Or is IPython used in a different way?

Yes, in the sense that while you are developing an application you are probably testing things out in a terminal. IPython makes that side of things a little easier. But it doesn't fundamentally change the workflow of developing an application.

1

u/avamk Mar 01 '15

That makes sense, thank you!

2

u/[deleted] Mar 01 '15

Have you ever just typed "python" at a terminal prompt and then messed around with simple Python statements? That's the Python interactive shell. IPython is just like Python, but with more goodies in the interactive shell. There are lots of videos available to give you an impression of what this is like.

By contrast, IPython notebook is web-based version of Python that allows you to create online notebooks that mix markdown formatted text, LaTeX, Python code snippets, plots, images, and much more. Again, an online video is probably the best way to get an idea of what it is like. The confusing bit about IPython notebook is that it is not a web site, per se, but a dedicated little web server that you can run on your own computer with the user interface being your regular web browser.

After you watch the videos you should try the online demo of the latest IPython notebook.

1

u/avamk Mar 01 '15

Have you ever just typed "python" at a terminal prompt and then messed around with simple Python statements? That's the Python interactive shell. IPython is just like Python, but with more goodies in the interactive shell.

Thanks that analogy (not sure if that's the right word...) is very helpful! Thank you!

OK, so the first video said something about IPython being "better than bash" in a number of ways. Wait, so IPython is also a general-use shell that can replace bash? Kind of like bash but with Python as a "language"??

I guess another follow up question to your answer is that since "IPython is just like Python", is it a drop in replacement for "regular" Python? I.e. Can I just install IPython from my package manager and not even bother with the usual Python?

2

u/hharison Mar 01 '15

OK, so the first video said something about IPython being "better than bash" in a number of ways. Wait, so IPython is also a general-use shell that can replace bash? Kind of like bash but with Python as a "language"??

I don't really agree with that characterization. One of the features of IPython is that you can run shell commands from the IPython console with the ! character. But it's not a better Bash any more than Python is a better Bash. It's a better Python terminal. For example, if you copy-paste code into it, it will fix the indentation for you (most of the time, if it can figure it out). If you put a question mark after a function, it will display its help. Things like that.

I guess another follow up question to your answer is that since "IPython is just like Python", is it a drop in replacement for "regular" Python? I.e. Can I just install IPython from my package manager and not even bother with the usual Python?

Yes, but if you do that your package manager will install Python as a dependency of IPython. Once you have IPython you never need to use regular Python directly, but every time you use IPython you are still using Python, just one relatively thin layer removed.

1

u/avamk Mar 01 '15

Aha! I think this is starting to make sense to me now in conjunction with /u/hharison 's answer.

Sounds fun, I think I'll give it a try as I continue to learn Python. Thank you!

2

u/[deleted] Mar 01 '15

It's a stretch to say it is better than Bash.

Having said that, it is was possible to literally use IPython as a replacement for your shell. I tried it for a few months and didn't enjoy it.