Getting Started With The Spyder IDE
Getting Started With The Spyder IDE
Claudius Gräbner1,2
1
Johannes Kepler University
2
ZOE. Institute for Future-Fit Economies
Abstract
Here I provide you with some basic information about the IDE Spyder, which will be used in
the video lectures.
1 Getting started
Spyder is an Integrated Development Environment. This means it combines many features associated
with programming tasks, including: the editing of text files, the execution of code in a shell1 , the
preview of figures created by your code, and much more.
Therefore, IDEs are very useful for programming. While there are alternatives to Spyder, I found
this to be an extremely intuitive, powerful, and freely available IDE.
This document should provide you with a very first orientation such that you can use Spyder
directly for the upcoming course videos.
After explaining how to start a project in Spyder (section 2) we will quickly go through the
most important elements of the graphical interface of Spyder (section 3), then learn how to exploit the
basic functionality such as code execution (section 4) and finally have a look at how you can customize
Spyder such that it serves you best (section 5).
1
You then need to decide whether you want to create a new folder for your project, or use an existing
one. You should definitely create the project as an ‘empty project’.
After having confirmed the creation of the project, you then see the standard Spyder graphical
interface, as shown below. We will now go through the most important parts of this interface one by
one below, but before I want to make a comment on how to organize all the files you will be creating
in the project folder during your work.
It is usually not a good idea to just save everything in the project folder. This will certainly create
a mess of files that will probably spoil your fun of working on your project. Rather, after setting up
the project folder, it is good to provide a structure for all the files that are about to create by creating
five new folders within your project folder:
◮ A folder called ‘src’ (abbreviation for ‘source’) or ‘code’ where you save all your Python scripts.
◮ A folder called ‘data’, where you store all the raw data that you are going to process during the
project.2
◮ A folder called ‘output’, into which you will save all the figures, tables, etc. that you are going
to produce during your project
◮ A folder called ‘text’ or ‘notes’ where you place all your text documents that you are writing
during this project
◮ A folder called ‘misc’, where you place all the rest, for example some slides with information
somebody did send you.
2
Of course, if you do not use any data as we do now in the beginning, you might skip it.
2
You do not need to follow the same convention, but you should think about a way of organizing
your files in a way that helps you to keep track of what is going on in your project. Placing all files
simply into one folder usually yields confusion and frustration, at least in the medium run.
Note that you can remove a pane by either clicking on the ‘X’ on its top-left corner, or by demarking
it in View → Panes . But more on customization below (section 5).
Here you can also create new folders, rename and delete files directly from within Spyder. Also, if
you right-click on a folder or a file you can open it directly in your explorer.
An important concept in this context is that of the working directory: the default working
directory of your Spyder session is your project folder. In my example here it is called ‘spyder-
intro’. This means, whenever you write a relative path, the path starts from your current working
3
directory. Although it is possible to change the working directory, I can think of no situation where
this is recommendable. Thus, I suggest you stay with your working directory, and use mainly relative
paths. Only use absolute paths when absolutely necessary because this usually prevents your code
from being used by others. /Users/graebnerc/work-claudius/general/teaching/abm-intro-dt/spyder-
intro/text/figs
/Users/graebnerc/work-claudius/general/teaching/abm-intro-dt/spyder-intro/
output/nice-plot.pdf
output/nice-plot.pdf
Often, it is more useful to use relative files: as you can see from the example, using relative paths
enables others to simply re-use your code once they set the same working directory. Because the
file structure, user names, and other folder names usually differ across machines, absolute paths
can cause problems when you give your scripts to someone else.
Although quite useful, I usually remove the project explorer since the file explorer (see below)
provides similar functionality. To remove the project explorer click on the top-left ‘X’ of its pane. If
you want to bring it back, set the corresponding mark at View → Panes → Project explorer .
4
Sometimes you might want to restart Python, or close the console and open a new one. You can
restart Python by clicking on the top right of the console pane (the weird wheel), and then click on
‘Restart kernel’. Or you can click on Consoles → Open an IPython console .
There is also a tab called ‘History log’. If you click on it you see a summary of all the commands
you have issued to Python in the current session.
This is sometimes a quite useful feature, but becomes quickly a bit overloaded. We will learn about
how to define variables shortly during the main video lectures.
The third tab is very helpful: whenever you call the help function, its output will be displayed
here. Alternatively, you can also search the documentation from this pane directly.
4 Basic functionality
We will now have a look at the most common functionality of Spyder that you are about to use.
If you want to create a new script you can do so by clicking on File → New file... . Then a
new script pops up in the script editor.
5
You can write Python code here and then save the script by clicking on File → Save or by using
the adequate shortcut (CMD+S for the Mac).
The most common ways to execute code refer to the following four buttons on the top of the
Spyder interface:
The first button is actually the least frequently used one: it executes the entire script that is
currently active.
The second and third button refer to a nice feature of Spyder: you can structure your script into
cells. This is done by writing #%% , followed by the title of the cell. Then, all lines before the next
cell definition will be wrapped into a cell, as indicated by the lines (and the code above will also be
wrapped into a cell):
If you click on the execution button on the middle-left, all the code in the current cell (i.e. the cell
where the cursor is located) gets executed. The button on the middle-right does the same but also
jumps into the next cell.
The button on the right executes the code that is currently marked, or, if not code is marked,
executes the line where the cursor is currently located.
Another nice feature of the editor is the possibility to add todos: If you write ‘# TODO’ into the
script, you will see a mark popping up at the side of the script: If you check Source → Show todo list ,
then you get an overview over all the todos you have given you. Below, you find a list of all warnings,
that Spyder issues whenever it suspects you made a coding error.
5 Customizing Spyder
There are many ways to customize Spyder to your needs, and I will only go through those that I found
most helpful in the beginning:
6
First, there are some general styles according to which the various panes can be allocated. For
those of you who are used to RStudio: there is also an RStudio style. You can change the style
View → Window layouts . Just experiment a bit until you find something that you like.
In case you experimented to much with the possible customization in View , you might rescue
yourself by using Tools → Reset Spyder to factory settings . This will undo all changes in the
settings you made.
Note that you can move the various panes just as you can move windows on your desktop.
There are a number of useful customizations available via python → Preferences . While I would
not change too much here in the beginning, particularly the preferences on the editor might be useful:
here you can change font size, color scheme, and much more.
Keep in mind that for some preferences affecting the console to have effect, you need to restart
kernel first