How to Install the Python Spyder IDE and Run Scripts
In this article, I’ll:
- Show you how to install Spyder on Windows and Linux.
- Guide you in starting a project and creating your first Python script.
- Explain how to use different Spyder tools while writing your Python code.
By the end of this post, you’ll know how to run an application in the Spyder IDE, how to add a package to it, and why it’s worth observing variable values in your Python script.
What do you need in order to install and start working with the Spyder IDE on your computer?
- An Internet connection.
- Basic knowledge of the Python programming language.
How to Install the Spyder IDE on Windows
Navigate to Spyder’s website and find the installer.
It’s recommended that you install the Anaconda distribution to get Spyder; this distribution contains some useful packages and an environment manager to keep your packages installed and up to date.
Select Download from the main menu, and then click on the Download Spyder with Anaconda button.
This will take you to a screen where you select your operating system for the installation. Click on the Windows icon.
You’ll be asked whether you’d like to download Python 3 or Python 2. We’ll go with the latest version of Python (which, as of this writing, is Python 3.7).
Once the installer has downloaded and you run it, the Setup window will display.
Click the Next button. In the License Agreement window, you’ll need to accept the terms by clicking the I Agree button.
Click Next to proceed through the rest of the windows.
Once you reach the page below, click the Install button.
The installation process will begin.
You can download Visual Studio Code if you’d like to use it. It’s a versatile IDE for developing in a variety of programming languages. We won’t install it in this article, though. Click the Skip button.
Once the installation is complete, click the Finish button.
Afterwards, go to Start › All programs (this is if you’re on Windows 7—if you’re on Windows 10, press the Windows key and look under Recently added). Select Anaconda Navigator.
You should see a window similar to the one below:
If you’d like run Spyder, just click on its Launch button. The IDE will open.
How to Install Spyder IDE on Linux
Navigate to Spyder’s website and find the installer.
It’s recommended that you install the Anaconda distribution to get Spyder; this distribution contains some useful packages and an environment manager to keep your packages installed and up to date.
Select Download from the main menu, and then click on the Download Spyder with Anaconda button.
This will take you to a screen where you select your operating system for the installation. Click on the Linux icon. I’ll be working with Ubuntu and downloading the installation file.
You’ll be asked whether you’d like to download Python 3 or Python 2. We’ll go with the latest version of Python (which, as of this writing, is Python 3.7).
If the file is successfully downloaded, you can use this command to start the installation with superuser privileges:
sudo bash path-to-installation-file
Enter your password for root.
Press Enter to continue.
The license agreement displays. Type yes to accept it, and press Enter.
You’ll see information about location path for Anaconda.
If you don’t want to change the default location for Anaconda, simply press Enter.
In the next step, type yes to add the path for Anaconda to your bashrc. Press Enter.
If you don’t want to install Microsoft Visual Studio Code, type no and press Enter.
The last thing to do is to type the following command to finish the installation:
source ~/.bashrc
Now we’re ready to run Anaconda Navigator. You do so by typing the following command:
anaconda-navigator
This will launch Anaconda Navigator.
Click the Launch button to run the Spyder IDE.
Spyder IDE Menu and Toolbar Overview
The main window of the Spyder IDE contains a main menu, toolbar, and several panes. At the top is the main menu:
Underneath it is the toolbar:
On the right side is an interactive console where you can write and run code:
On the left is an editor where you can write and save Python scripts:
To the left of the editor is the Project Explorer pane, which displays all project nodes with their related folders and files:
How to Start a Project in the Spyder IDE
In the Spyder IDE, as in other IDEs, we can create a project to organize our files, like images, scripts, and related text files. To create a project, simply select Projects › New Project from the main menu.
The Create new project window will display. Fill in the information for your project, including its name and directory (if the selected directory already exists, make sure the “Existing directory” radio button is selected at the top). When you’re done, click the Create button. Here, we chose the name “MyFirstProject.”
You’ll see the project folder in the Project explorer pane.
Writing Your First Python Script in Your Project
Every project contains files: code, pictures, libraries, text files, and more. We’ll create a file that allows us to write a Python script. Right-click on your project folder, and choose New › File.
The New File dialog displays. Enter the name of your script (ours is MyFile), and click Save. Remember to add a .py extension at the end of name of the file name to designate it as a Python script.
Your file will appear under your project folder in the Project explorer pane of the IDE.
The file will also automatically be opened in the Editor pane. Here, we can write Python code, like this:
print('This is my first script in Python')
You can then save your file by pressing Ctrl+S or choosing File › Save from the menu.
Spyder IDE Features
Most IDEs help you write better code. The Spyder IDE is no different!
Comments
One of the available features allows you to quickly add block comments to your code. Click on the line where you’d like to insert a block comment, and then select Edit › Add block comment from the menu.
This will insert a block comment like so:
In a similar way, you can remove block comments using the Edit menu—just click inside the comment block, and choose Remove block comment.
Syntax Highlighting
If you look at the code in your editor, you’ll see many different colors: strings are in green, brackets in bright green, the names of functions are violet. This is known as syntax highlighting—a feature that most text editors have to make it easier for you to find relevant parts of your code. We can use the default highlighting rules or specify your own.
To begin, go to Tools › Preferences.
The Preferences window displays. Choose Syntax coloring on the left, and click the Edit selected button on the right.
You’ll see the Color scheme editor, which lets you change the colors for different parts of your code. After making changes, click the OK button.
Syntax Warnings and Auto-Completion
Now let’s explore another feature of the IDE. Here, we’ve created a new script named Editors.py and added it to our project. It’s currently open in our Editor pane.
If you make a syntax mistake in your code (here, omitting a closing bracket for a list), the IDE will inform you of it with a red x icon. You can hover your mouse over it for more information.
Spyder IDE also provides a completion feature during coding. If you start writing a Python keyword and forget the rest of the spelling, the IDE will offer a list of suggestions. For example, if we type only the character i and press Tab or Ctrl+Space, we’ll see a dropdown list of keywords starting with the letter 'i'. You can choose one of the suggestions or continue typing.
How to Run Python Code in the Spyder IDE
We can run project files using the green Run button from the toolbar. Alternatively, you can select Run › Run from the main menu or use the shortcut key (F5).
You’ll see the result of running your currently active file in the Console pane:
However, sometimes we make non-syntactic errors when coding. In this situation, after running your code, you’ll see the error summary in the Console pane.
Of course, as in most IDEs, you can also debug your program. There’s a group of debug buttons that allow you to run an application and debug it step by step to find errors.
How to Add a Module/Package to Spyder IDE
The Spyder IDE contains large a set of packages and modules for Python. A module is a file written in Python (with the .py extension) that contains classes, functionses, or variables to use in your Python program. Multiple modules may be grouped in a package.
If we’d like to use a package in our code, we first need to install it. For example, if you want to try to import the logbook package into your script without the package installed, it will not work. Take a look at the example code below:
The console informs us of the error: No module named ‘logbook’.
The solution is simple! All you have to do is go to your Spyder console and type the following command:
!pip install logbook
You’ll see a message about the installation process:
Now when we run the example file that imports the logbook package, it will work without errors.
The Spyder IDE Variable Explorer: Why Use It?
The Spyder IDE also allows you to observe the values of variables in your script. By default, this information is in the top-right pane titled Variable explorer. In the example below, we see the values of variables in the script that we ran on the left.
The variable explorer allows us to not only see the values of variables but also edit those values.
Additionally, you can select variables from the explorer pane and choose Plot or Histogram from the menu to create a plot from that set of data.
How to Check Your History Log in Spyder
The history log is a pane that contains summary logs from your last session. To open it, click the History log tab below the console. In this pane, the console will show all commands you ran with accompanying timestamps.
Summary
This short overview of Spyder IDE is only a sneak peek at the editor’s main features. Hopefully, this helps you start your first Python project in Spyder IDE and verify whether the editor is a good solution for you.
However, you shouldn’t stop here. You can extend your Python language skills with Vertabelo Academy’s interactive online courses on Python:
- Python Basics. Part 1 (programming)
- Python Basics. Part 2 (programming)
- Python Basics. Part 3 (programming)
- Introduction to Python for Data Science (data science)
- Working with Strings in Python (data science)
The best way to learn Python is by doing. So get started today!