
QGIS Python Programming Cookbook, Second Edition
By :

QGIS has a set of Python modules and libraries that can be accessed from the Python console within it. However, they can also be accessed from outside QGIS to write standalone applications. First, you must make sure that PyQGIS is installed on your platform, then set up some required environment variables.
In this recipe, we will walk you through the additional steps required beyond the normal installation process to prepare your system for development. The steps for each platform are provided, which also include the different styles of Linux package managers. The version of QGIS in your Linux package manager may be older. You can find additional detailed installation instructions at https://www.qgis.org/en/site/forusers/alldownloads.html.
QGIS uses different installation methods for Windows, GNU/Linux, and Mac OS X. The Windows installer installs everything you need for Python development, including Python itself.
However, on both Linux distributions and Mac OS X, you may need to manually install the Python modules to complete the installation process. On Mac OS X, you can download installers for some commonly used Python modules with QGIS; refer to http://www.kyngchaos.com/software/python.
On Linux, you have an option to either compile from the source or just specify the Python interface to be installed through your package manager.
sudo apt-get update
sudo apt-get install qgis python-qgis qgis-plugin-grass
sudo yum update
sudo yum install qgis qgis-python qgis-grass
Now we need to set PYTHONPATH
to the PyQGIS
directory. At the same time, append the path to this directory to the PATH
variable so that you can use the PyQGIS modules with an external IDE.
PYTHONPATH
variable in a command prompt to the bin
directory of the QGIS installation: set PYTHONPATH="C:\Program Files\QGIS 2.18\bin"
bin
directories to the system's PATH
variable: set PATH="C:\Program Files\QGIS 2.18\bin";"C:\Program Files\QGIS
2.18";%PATH%
PYTHONPATH
variable in a command prompt to the bin
directory of the QGIS installation: export PYTHONPATH=/usr/share/qgis/python
export LD_LIBRARY_PATH=/usr/share/qgis/python
The QGIS installation process and package managers set up the Python module's configuration in a way that it becomes internal to QGIS. When you use the Python console inside QGIS, it knows where all the PyQGIS modules are. However, if you want to use the PyQGIS API outside it by using a system Python installation on either Windows or Linux, it would be necessary for you set some system variables so that Python could find the required PyQGIS modules.
This recipe uses the default QGIS paths on each platform. If you aren't sure which PyQGIS path is for your system, you can figure this out from the Python console in QGIS.
The libraries on Windows, as opposed to other platforms, are stored in a different location. To locate the path, you need to check the current working directory of the Python console:
os
module to get the current working directory:import os os.getcwd()
Perform the following steps to find the path needed for this recipe on all the other platforms:
sys
module to locate the PyQGIS pathimport sys sys.path
/python
, which is the location of the Python installation used by QGIS.