Sunday, July 31, 2016

How to Install Third-party Python Modules in QGIS (Windows)

How to Install Third-party Python Modules in QGIS 2.12.3-Lyon (Windows)

If you have installed QGIS, you will see that it bundles its own version of Python in: C:\Program Files\QGIS\apps\Python27. This is the Python that is used when calling python expressions on the QGIS Python Console.

Now we needed to make use of a library called: Python Spatial Analysis Library (PySAL). Then  we entered "import pysal" on the python console but surprisingly, we got an error that the module named pysal doesn't exist. That is TRUE, because it isn't installed with our QGIS python installation! So how do we install this library?



The easiest way install Python libraries is to use 'pip'. 'pip' is tool for installing and managing Python packages. 'pip' is a better package manager then easy_install and a replacement for easy_install

However at the moment of writing, the python setup (version 2.7.5) that comes with QGIS 2.12.3-Lyon installation doesn't have "pip" installed. Am sure in the future releases, an updated version of python (with 'pip' installed by default) will be use.

So now that we know that our QGIS python setup doesn't have 'pip', then it means we have to install 'pip' before we can install a third-party module/library.

The steps to take are as follow:-

2) Locate where you installed the QGIS, it cold be at; "C:\Program Files\QGIS Lyon"



3) You should see "OSGeo4W shell", right click on it and select "Run as Administrator".


4) Take note of the PATH "C:\Windows\System32". Copy "ez_setup.py" you downloaded above to this location


5) Now run "python ez_setup.py" on the OSGeo4W shell. This will install "easy_install" which you will use to install 'pip' in the next step.



6) To install a 'pip' package with easy_install just run "easy_install pip" on the OSGeo4W shell.



7) That is it. You should now be able to install python packages on QGIS python setup by running: pip install <package_name>

For example lets install a Python Spatial Analysis Library (PySAL). Open your QGIS OSGeo4W shell and and run: pip install pysal



Now if you launch your QGIS software and import pysal on the python console, you will not get import error and you should be able to use all the PySAL classes as seen below.



That is it.
Hope this helps.

No comments:

Post a Comment