Back to articles list Articles
9 minutes read

How to Install the Python Spyder IDE and Run Scripts

One of the most popular Python IDEs is Spyder, which stands for Scientific PYthon Development EnviRonment. This Python tool is "designed by and for scientists, engineers and data analysts." But it's not just for professionals. In this article, we'll learn why the Spyder IDE is also an excellent tool for beginners.

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?

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.

Spyder installation

This will take you to a screen where you select your operating system for the installation. Click on the Windows icon.

Spyder installation

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).

Spyder installation

Once the installer has downloaded and you run it, the Setup window will display.

Spyder installation

Click the Next button. In the License Agreement window, you'll need to accept the terms by clicking the I Agree button.

Spyder installation

Click Next to proceed through the rest of the windows.

Spyder installation
Spyder installation

Once you reach the page below, click the Install button.

Spyder installation

The installation process will begin.

Spyder installation
Spyder installation

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.

Spyder installation

Once the installation is complete, click the Finish button.

Spyder installation

Afterwards, go to StartAll 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:

Anaconda Navigator

If you'd like run Spyder, just click on its Launch button. The IDE will open.

Spyder

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.

Download Spyder with Anaconda

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.

Spyder installation

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).

Spyder installation

If the file is successfully downloaded, you can use this command to start the installation with superuser privileges:

sudo bash path-to-installation-file

Spyder installation

Enter your password for root.

Spyder installation

Press Enter to continue.

The license agreement displays. Type yes to accept it, and press Enter.

Spyder installation

You'll see information about location path for Anaconda.

Spyder installation

If you don't want to change the default location for Anaconda, simply press Enter.

Spyder installation

In the next step, type yes to add the path for Anaconda to your bashrc. Press Enter.

Spyder installation

If you don't want to install Microsoft Visual Studio Code, type no and press Enter.

Spyder installation

The last thing to do is to type the following command to finish the installation:

source ~/.bashrc

Spyder installation

Now we're ready to run Anaconda Navigator. You do so by typing the following command:

anaconda-navigator

Spyder installation

This will launch Anaconda Navigator.

Spyder installation

Click the Launch button to run the Spyder IDE.

Spyder installation

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:

Spyder

Underneath it is the toolbar:

Spyder

On the right side is an interactive console where you can write and run code:

Spyder

On the left is an editor where you can write and save Python scripts:

Spyder

To the left of the editor is the Project Explorer pane, which displays all project nodes with their related folders and files:

Spyder

How to Start a Project in the Spyder IDE

Spyder

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 ProjectsNew 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."

Spyder

You'll see the project folder in the Project explorer pane.

Spyder

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 NewFile.

Spyder

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.

Spyder

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')
Spyder

You can then save your file by pressing Ctrl+S or choosing FileSave from the menu.

Spyder

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 EditAdd block comment from the menu.

Spyder

This will insert a block comment like so:

Spyder

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 ToolsPreferences.

Spyder

The Preferences window displays. Choose Syntax coloring on the left, and click the Edit selected button on the right.

Spyder

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.

Spyder

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.

Spyder

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

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.

Spyder

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 RunRun from the main menu or use the shortcut key (F5).

Spyder
Spyder

You'll see the result of running your currently active file in the Console pane:

Spyder

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.

Spyder

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:

Spyder

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:

Spyder

Now when we run the example file that imports the logbook package, it will work without errors.

Spyder

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.

Spyder

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.

Spyder

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 LearnPython.com's interactive online courses on Python:

The best way to learn Python is by doing. So get started today!