Skip to Main Content
University of Texas University of Texas Libraries

GIS & Geospatial Data Services

QGIS and Python

Getting Started with Python and QGIS

Getting started with using Python to automate QGIS workflows can be a bit of a challenge at first, but fortunately the resources below can help accelerate the learning process and are really helpful to refer to even if you are an expert Python user. The PyQGIS cookbook is the best place to start for learning how to utilize the QGIS Python libraries in your scripts while the PyQGIS API documentation is an extremely useful comprehensive guide that describes every class and method that the API provides access to.

PyQGIS Introductory 'Cook Book':
https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/

PyQGIS 3.x API Documentation:
https://qgis.org/pyqgis/

PyQGIS 2.x - 3.x API Break Documentation:
https://qgis.org/api/api_break.html
If you are looking for general Python documentation and tutorials to help build up your Python skills in preparation for GIS related scripting you may want to visit the following websites which offer useful resources to Python learners.

Python.org Beginners Guide:
https://wiki.python.org/moin/BeginnersGuide

W3 Schools Python Tutorial:
https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/

LinkedIn Learning Python Video Lessons:
https://www.linkedin.com/learning/topics/python

Configuring QGIS and Python on a Windows Computer
Please note that portions of the text that are colored red in the instructions below should be modified after copying and pasting to reflect the correct version of QGIS that you have installed on your computer.
  1. Install the standalone QGIS application (not the OS Geo bundle with QGIS) from https://qgis.org/en/site/forusers/download.html if you have not done so already
  2. Identify the path to the Python executable that was installed with QGIS (it should be at C:\Program Files\QGIS 3.14\apps\Python37). Navigate to this location in your file system and make a duplicate copy of the python.exe file. Name this duplicate copy of the Python executable python3qgis to distinguish it from other installations of Python that might already exist on your computer.
  3. In the search bar located in the left portion of the task bar at the bottom of your screen, search for "advanced system settings" and then click the "View Advanced System Settings" button that shows up in your results. In the window that appears, click on "Environmental Variables...". **Please note that you will need administrative privileges on the computer you are working on in order to complete this step.
  4. In the Environmental Variables window, click on the row for the Path variable under User Settings to highlight it in blue. Add the path to the QGIS Python executable (which you copied in step 2 above) to your path, by clicking the Edit button, than the New button, and pasting in the QGIS Python executable path in the blank row that your cursor has been moved to. Once you have done this click OK to return to the Environmental Variables window
  5. Now click the New button under User Variables. In the new window that appears, type PYTHONPATH for the Variable Name and paste C:\Program Files\QGIS 3.14\apps\qgis\python;C:\Program Files\QGIS 3.14\apps\qgis\python\plugins;C:\Program Files\QGIS 3.14\apps\Qt5\plugins;C:\Program Files\QGIS 3.14\share\gdal; in the Variable Value field, then click OK.
  6. Now click OK in the Environmental Variables window and in the System Settings window to save the changes that you've made.
  7. To verify your QGIS Python instance can now be run outside of the QGIS environment and used to develop external scripts, open a new Command Prompt window. In this new window type python3qgis. This will start Python in the Command Prompt environment. Now type from qgis.core import * and press Enter on your keyboard. If you see your Command Prompt jump down to a new line after a few seconds without generating an error than you have verified that you are able to QGIS Python libraries in your script.
  8. You should now be able to use any integrated development environment (IDE) software to write a Python script that utilizes QGIS Python libraries. To make sure that your IDE calls the correct Python executable for any scripts that you write, make sure the path to its default Python installation is the path python3qgis Python executable that was created in step 2 above. Before starting a new script make sure that you refer to https://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/intro.html#using-pyqgis-in-standalone-scripts as this contains essential information about structuring your script to ensure that the QGIS Python libraries can be used successfully. If you do not already have an IDE application installed that can be used for Python development you can continue with the steps below which describe how to install and use Atom to create and run Python scripts that reference QGIS Python libraries.
  9. Visit https://atom.io/ and click the Download button to download the Atom installation files on your computer. Double click on the installion file to install Atom.
  10. After Atom has finished installing, open the application. Click on File > Settings to open the application settings window. In the Settings window, click the Install button and then in the search bar that appears on the Install page type atom python run. The atom-python-run package should be the first one that appears in a list of search results. Click the Install button for the atom-python-run package. Once it is installed, click the Settings button for this package. On the package settings page find the text field under the F5 Comman heading, and paste in the following text: python3qgis {file}. This configuration will allow you to easily run a script that you are working on in Atom using the python3qgis Python executable on your computer by hitting the F5 button on you keyboard.
  11. Now that the atom-python-run package has been configured, click File > New File to create a new blank Python script file for you to work in. This file will open in a new tab within Atom. As soon as it opens click FIle > Save to open the file save window. In this window selection the location on your file system where you want the script to be stored and select a name for you script. Please note that the file name must end in .py in order for it to be correctly treated as a Python file.
  12. Once your script has been successfully saved, you can now start to write your Python code in the file. If you want to verify that everything is working correctly type the following for your first line of code: from qgis.core import *. Next, hit the F5 button on your keyboard to automatically save and run the script (please note you may need to make sure that the Fn button on your keyboard is on for the F5 key to call the function correctly). If everything is working properly you should see a Python shell appear. After a few seconds the text Process returned 0 (0x0) execution time : 0.123 s should appear in the shell if the code was executed successfully and the QGIS core library was imported without any issues. If instead of this message you notice an error or the Python does not appear at all when you hit F5, please review the instructions above to look for any place where a step might have been missed. If everything is working correctly you can now proceed with writing your script
Configuring QGIS and Python on a Mac
Please note that portions of the text that are colored red in the instructions below should be modified after copying and pasting to reflect the correct version of QGIS that you have installed on your computer.
  1. Install the latest version of the standalone QGIS application from https://qgis.org/en/site/forusers/download.html if you do not already have it on your system. Please not that these instructions may not work for older version of QGIS installed on a Mac.
  2. Go to your applications folder, right click on QGIS, click "Show Contents", and navigate to /Applications/QGIS.app/Contents/MacOS/bin/ to make sure that this path exists
  3. Open terminal and paste in:
    /Applications/QGIS.app/Contents/MacOS/bin/python3.8
    then hit Enter on your keyboard. This should open the Python interpreter that was installed with QGIS in your terminal. Now that you have activated the Python interpreter in the Terminal, type the following:
    from qgis.core import *
    then hit Enter on your keyboard. If you don't receive an error after running this line of code, that is an indication that the path to your QGIS Python interpreter is correct and your Python environment is successfully configured.
  4. Visit https://atom.io/ and click the Download button to download the Atom installation files on your computer. Double click on the installion file to install Atom.
  5. After Atom has finished installing, open the application. Click on File > Settings to open the application settings window. In the Settings window, click the Install button and then in the search bar that appears on the Install page type atom python run. The atom-python-run package should be the first one that appears in a list of search results. Click the Install button for the atom-python-run package. Once it is installed, click the Settings button for this package. On the package settings page find the text field under the F5 Command heading, and paste in the following text: /Applications/QGIS.app/Contents/MacOS/bin/python3.8 {file}. This configuration will allow you to easily run a script that you are working on in Atom using the QGIS Python installation on your computer by hitting the F5 button on you keyboard.
  6. Now that the atom-python-run package has been configured, click File > New File to create a new blank Python script file for you to work in. This file will open in a new tab within Atom. As soon as it opens click FIle > Save to open the file save window. In this window selection the location on your file system where you want the script to be stored and select a name for you script. Please note that the file name must end in .py in order for it to be correctly treated as a Python file.
  7. Once your script has been successfully saved, you can now start to write your Python code in the file. If you want to verify that everything is working correctly type the following for your first line of code: from qgis.core import *. Next, hit the F5 button on your keyboard to automatically save and run the script (please note you may need to make sure that the Fn button on your keyboard is on for the F5 key to call the function correctly). If everything is working properly you should see a Python shell appear. After a few seconds the text Process returned 0 (0x0) execution time : 0.123 s should appear in the shell if the code was executed successfully and the QGIS core library was imported without any issues. If instead of this message you notice an error or the Python does not appear at all when you hit F5, please review the instructions above to look for any place where a step might have been missed. If everything is working correctly you can now proceed with writing your script
  8. edit the settings for atom python run and for the F5 key set the configuration to

GIS Questions?

Profile Photo
Michael Shensky

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 2.0 Generic License.