How to Setup pyQGIS with Spyder

Prasanna Wijesiriwardana
4 min readApr 23, 2020

I recently started learning about QGIS and drawing vector layers of points, lines, or polygons using QGIS for a new project. After spending few hours familiarizing with QGIS, I came across pyQGIS and was excited to see , many QGIS related actions can be automated using the Python API.

The Python console in the QGIS application is a very simple one. It has few basic functionalities such as execute, new script, open a script, find, comment/uncomment code etc. To write or execute a simple script above functionalities are sufficient. However, working with large sophisticated codebases, in QGIS python console, was difficult due to limited functionalities.

I started working with python couple of years back and since then always used Anaconda distribution and Spyder IDE for my developments. It might not be the best python IDE but its my go to tool for python. So I wanted to use Spyder IDE to work with QGIS scripts as well.

As I intended to work with QGIS, I opened the Spyder and started with importing a QGIS library. When I executed import script, even though QGIS was already installed, greeted with a module not found error. Then I realized QGIS installation comes up with a separate python installation to handle python codes. Whereas the Spyder IDE is linked to the global Python installation.

There are few options to overcome this.

  1. Install QGIS libraries in default Python environment
  2. Create virtual environment and install required libraries

I chose the second option due to the reasons explained below.

1. Install QGIS libraries in default Python environment

conda install -c conda-forge qgis

This method is very straightforward as it adds required QGIS packages to the same python environment where your Spyder IDE is linked to. However this method might not work for everyone, especially if you are running some critical operations using global environment. As new package installations may downgrade or upgrade critical dependent packages in the global environment, it might affect existing Python applications which require specific versions.

2. Create virtual environment and install required libraries

For every time a new package needs to be tested this is my go to method. A virtual environment is a self-contained directory tree which has an independent python installation and many required packages isolated from global environment or any other virtual environment on the system.

Okay, Lets see how to implement this step by step. Please note I’m using anaconda prompt to set up the environment.

First let’s create a virtual environment.

conda create — name qgis_env

Once created , activate the newly created virtual environment.

conda activate qgis_env

Once activated, prompt will change to the environment you are in. Now the changes will be affected only to the virtual environment. Lets install all the required packages to the virtual environment.

Lets start by installing QGIS

conda install -c conda-forge qgis

Now QGIS and dependent packages are installed. We can try importing qgis.core library and see whether its loading in the virtual environment. To test that we can use the python console.

It successfully load the qgis.core library. So how can we use Spyder in this environment?

I tried connecting the installed Spyder to the newly created virtual environment without any luck. If someone knows how to do that, please let me know.

After researching a bit about this, I realized installing Spyder in the newly created virtual environment can solve this.

conda install spyder

Once installation is successful, all you have to do is start Spyder in the Virtual environment.

and Voila!

Now I have a Spyder IDE and QGIS both setup and fully functional.

Please let me know if this post is helpful or how I can improve this any further.

Cheers and happy coding!

I have not written a blog post almost in 8+ years and here I am giving another shot at writing blogs. Medium is one of the main motivations behind it, as I really loved its easy and minimalist approach towards writing.

Thanks for that.

--

--