Scientific development environments: IPython and Positron#

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

The default command-line interpreter comes with some functionality (for example, if you use the up and down arrows you can navigate to 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: interactive Python#

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 to the standard Python interpreter? The >>> prompt has been replaced by In [1]:. Now there is a slight difference between a line with and without print() (can you detect it?). At first sight the differences are small, but it is in the everyday interactive coding that ipython becomes useful.

From now on, we will always use the IPython command-line interpreter. Some of its most useful capabilities are described below.

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]. Now the only possible option 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 into your IPython interpreter:

s = 'the jungle book'
print(s)
l = sorted(s)
print(l)
l = ''.join(l)
print(l)

Note that [CTRL-V] does not work in a Linux terminal: you will 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 give them the 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 to %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 more about this later.

Integrated Development Environments#

An integrated development environment (IDE) is software for programming and software development that typically allows you to edit and compile your code in one environment. It usually includes syntax highlighting, syntax checking, an integrated terminal, debugging support, and version control. IDEs for scientific data analysis also often provide interactive exploration of variables and an integrated plot panel.

Commonly used (and free) IDEs for Python include

In the end, which IDE is best, depends on the requirements and personal preferences. In this class, we are going to use a fairly new one: Positron, which provides the capabilities of a modern programming environment, but is easier to get started with than, e.g., PyCharm. If you prefer to work with another IDE, that’s fine, as long as it works with Python scripts (not only Jupyter Notebooks) and it provides Gitlab integration (or you handle the git version control manually via the terminal).

Positron#

Positron is a free IDE for scientific Python (and R), well suited for analysing datasets. It provides a modern programming environment, including git integration, handling of Python environments (works with conda environments), support of Jupyter Notebooks, and AI-assisted coding.

To get started, download the installer for your platform from the download page.

To familiarize yourself somewhat with the user interface:

  • Open the script my_python_script.py and change some of the numbers.

  • Set up the Python interpreter (top-right corner): select your scipro environment.

  • Run the script - you have multiple options: (i) use the arrow in the top-left corner of the editor, (ii) use %run in the IPython interpreter (tab CONSOLE in the bottom panel) - this is also what happens if you press the arrow button (option i), or (iii) use the standard Python interpreter from the terminal (tab TERMINAL in the bottom panel).

You will quickly get familiar with the interface and additional features over the next few weeks.

Learning checklist#