Scientific development environment: ipython and spyder#
Reminder: the “standard” python interpreter#
Let’s open a python interpreter and type in some commands:
>>> a = 2
>>> b = 3
>>> print(a+b)
5
>>> a*b
6
>>> a**3
As it turns out, you will rarely use the python interpreter, but actually used a “more advanced” one: ipython
ipython: interactive python#
The default command-line interpreter comes with some functionality (for example, if you use the up and down arrows you can navigate in your previous commands) but this functionality is very sparse. Some important functionalities are missing: some are cosmetic (like colors), some are very useful (like automatic code completion with [TAB]
or the possibility to paste several lines of code at once). For all these reasons, the default python command-line interpreter is only used for very small tasks like testing a command or two. Scientists need more interactivity when analyzing their data: this is why ipython
was created.
ipython is a standard command-line interpreter, but enhanced with many interactive features. Let’s open it (ipython
command) and type in some commands:
In [1]: a = 2
In [2]: b = 3
In [3]: print(a + b)
5
In [4]: a*b
Out[4]: 6
What are the differences? The >>>
prompt has been replaced by In [1]:
. Now there is a slight difference between a line with or without print()
(can you find it?). At first sight the differences are small, but it’s in the everyday interactive coding that ipython
becomes useful.
From now on, we will always use the ipython command-line interpreter. I will now describe a small sample of its most useful capabilities.
Automatic code completion#
In ipython, type s
and then [TAB]
. The tool will give you a list of suggestions to choose from (useful when you forgot how a function was called exactly!). Now type so
and [TAB]
. Here the only solution will be selected automatically.
Getting help#
Type sorted?
. A text help should be printed, telling you what this function is about.
Copy paste#
Copy the following lines and paste them in your ipython interpreter:
s = 'the jungle book'
print(s)
l = sorted(s)
print(l)
l = ''.join(l)
print(l)
(note that [CTRL-V]
doesn’t work in a linux terminal: you’ll have to use [CTRL-SHIFT-V]
, the mouse with right click -> paste
, or - even better - the magical linux middle mouse button)
Running scripts#
Reminder: running a python script from the terminal#
Python scripts are simple text files. Per convention we like to give them a suffix: .py
. You can run python scripts by calling python in a terminal:
$ python my_python_script.py
Running a python script from the ipython interpreter#
Running a script from the ipython interpreter is as easy as:
%run my_python_script.py
The product of 3 and 6 is 18
(remember the [TAB]
functionality in ipython? You can use it on files as well). Running a script this way is different from running a script in the terminal. Indeed, after running the script, the variables defined in it are still available in the current session. Try computing a + b
to check it out:
a + b
9
Importing a python file#
Another way to “run” a script in a python interpreter is via the import
mechanism:
import my_python_script
The product of 3 and 6 is 18
Although both %run
and import
execute the script (the output is printed on screen), the two mechanisms are different. As we are going to see, the import
mechanism is an important feature of the python language (also something which you might not be used to from other languages). import
is usually not used as a replacement for %run
. The biggest difference with %run
is that the variables defined in the script are not available at the command line directly. Instead, they can now be addressed as following:
my_python_script.a * my_python_script.b
18
… but this is already going too far: more on this in the next lesson.
Spyder#
Spyder is an open-source integrated development environment (IDE) for scientific pythom, and it is the best for beginners. To open spyder, simply open your conda environment and type:
spyder
This should open a window like the above.
I think that you will learn spyder fast enough while coding, but if you want some tips to get started, the official documentation is a good place to go first!
Learning checklist#