eyecatch

Automate QGIS: How to Use the Python Console

Published: Last updated:
This article uses QGIS 3.34. The current LTR is 3.44.

What you'll learn


  • How to use Python in QGIS
  • How to automate processing in QGIS

Recommended for


  • Anyone who is getting tired of repeating operations on screen in QGIS
  • Anyone who wants to run several analysis processes together

Introduction

This article explains the Python console feature in QGIS and shows how to run Python code with the interactive console and the code editor. The same content is also covered in a video, so watch it for detailed instructions.

This article is a revised version of the “QGIS Blog” content that was published on the MIERUNE Inc. website.

What is the Python console?

The Python console is a feature for running Python from an interactive console and a code editor, as used in QGIS plugin development. It is useful for automation too small to need a plugin, and for checking how PyQGIS, the QGIS API for Python, behaves.

How to use the interactive console

First, open the Python console. In the menu bar, click Plugins → Python Console.

Open the Python Console
Open the Python Console

The Python Console panel opens at the bottom of the QGIS window.

The Python Console appears at the bottom of the window
The Python Console appears at the bottom of the window

Enter a Python command in the input area (area 7) and the result appears in the output area (area 8), so you can run commands interactively, as in a command-line interface. When the output grows long and becomes hard to read, click the Clear Console button (button 1) to clear the output area. You can also change the console's look by changing the theme in Options.

The table below describes each function.

No. Function Description
1 Clear Console Deletes all text and output shown in the Python console.
2 Run Command Runs the Python code you entered.
3 Show Editor Shows the built-in QGIS editor.
4 Options Opens the settings for the Python console.
5 Help Shows help documentation related to the Python console and PyQGIS.
6 Dock Code Editor Docks (fixes) the code editor to the QGIS main window.
7 Output area The part of the Python console where output (results, error messages, and so on) appears.
8 Input area The area for entering code in the Python console.

How to use the editor

In the editor, you can write, run, and save Python scripts. It is also useful when you want to try out a process you plan to implement, including in QGIS plugin development. In particular, because the editor and the console share the same scope, you can change the arguments of a function defined in the editor and test it interactively.

The editor UI
The editor UI

The table below describes each function.

No. Function Description
1 Open Script Loads an existing Python script file into the editor.
2 Open in External Editor Opens the selected script file in an external text editor.
3 Save Saves the script currently shown in the editor.
4 Save As Runs the script in the editor.
5 Run Script Runs the script in the editor.
6 Cut Cuts the selected text.
7 Copy Copies the selected text.
8 Paste Pastes the text saved in the clipboard into the editor.
9 Find Text Searches for text in the editor. Useful for finding a specific word or part of the code.
10 Toggle Comment Comments out or uncomments the selected lines.
11 Reformat Code Tidies up the formatting of the code.
12 Object Inspector A tool for inspecting the objects and variables in a script.
13 Editor The editor itself. Enter and edit code here. Write and modify scripts in this editor.

Python sample code

Now use Python to perform some simple layer operations. The data is the Lake Data from National Land Numerical Information. Download it and add it to QGIS.

Lake layer displayed
Lake layer displayed

On the lake layer you added, get the number of features in the layer and zoom to a feature selected with an expression. Enter the code below in the editor and click Run Script.

# Get the lake layer by specifying the layer name
layer = QgsProject.instance().mapLayersByName('W09-05-g_Lake')[0]
# Print the number of features
print(layer.featureCount())
# Select the feature whose W09_001 field value is Lake Sapporo
layer.selectByExpression('"W09_001"=\'さっぽろ湖\'')
# Store the first selected feature in a variable
selected_feature = layer.selectedFeatures()[0]
# Store the map canvas in a variable
canvas = iface.mapCanvas()
# Zoom to the selected feature
canvas.setExtent(selected_feature.geometry().boundingBox())
# Refresh the canvas
canvas.refresh()

The map canvas zooms to Lake Sapporo (さっぽろ湖), and the output area shows 556, the number of features in the lake layer.

Output result
Output result

Conclusion

This article gave a brief overview of how to use the Python console. The console runs code easily and lets you work with layers, the map canvas, and more. It is a very useful feature that is easy to recommend. Give it a try.

About the author
QGIS LAB Editorial Team
QGIS LAB Editorial Team

QGIS LAB is a comprehensive information hub for QGIS, the open-source GIS software. Under the concept of “Geospatial for Greater Good,” we share the knowledge and skills to open up the world through location data.