December 16, 2024

How to Configure and Run Spyder Python Editor in Virtual Environments

What is covered in this tutorial: In this Python tutorial, we explain how to correctly run Spyder Python editor in virtual environments and how to install custom Python packages in Spyder.

Motivation: Although I am big fan of VS Code, I still think that Spyder is easier for the development of scientific software and for debugging. Maybe this is my personal preference due to the fact that I spent a significant amount of time during my PhD working in MATLAB. Since Spyder is developed for scientific computing, for me, Spyder seems like a natural Python development environment.

On the other hand, my suggestion is to use virtual environments whenever you can. By using them, you can have precise control of all the packages that your software relies upon. You can be confident that your software will work on other platforms as long as the required packages and their versions are installed from scratch in the virtual environment. Also, by using virtual environments you can avoid version conflicts with the base Python or Anaconda environments.

However, I have struggled a lot to figure out how to properly run Spyder inside of user-defined virtual environments. Finally, I figured out how to properly configure Spyder such it can recognize the virtual environment and execute the code inside of the virtual environment. I am sharing with you my knowledge and experience, so you can save time and become a more productive developer.

The YouTube tutorial is given below.

Create a Python Virtual Environment and Configure Spyder To Run Inside

First, let us create a workspace folder and a Python virtual environment. Open a Windows Command Prompt and type

cd\
mkdir codes
cd codes
mkdir testSpyder
cd testSpyder 

Here, let us crate a Python virtual environment:

python -m venv env1

Then, let us activate that environment by typing:

env1\Scripts\activate.bat

As a practice, let us a NumPy package

pip install numpy 

Next, we need to figure out the path and location of the Python executable file inside of our environment. To do that, we need to type this:

python -c "import sys; print(sys.executable)"

This command will list the location of the Python executable file that is run inside of our environment. The output is given below

The Python interpreter file is located in this folder

C:\codes\testSpyder\env1\Scripts>

Memorize this path since you will need it in Spyder.

Next, open Spyder, and click on Tools, and Preferences

You will obtain the menu shown below

Then, select the option “Use the following Python interpreter:”

and enter the path to the Python interpreter inside of the created environment. Then, click OK. Next, for these changes to take the effect, click on Consoles, and click on Restart kernel

After doing this, you will get this error

To fix this error, go back in the terminal, and type

pip install spyder-kernels==2.4.*

Then, close Spyder, and open Spyder again. Then, type the test code and run it inside Spyder.

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import numpy as np


a=np.array([[1,2],[3,4]])


b=np.array([[1,2],[3,4]])

c=np.matmul(a,b)

You will be able to run it inside of the created Environment.