Sunday, July 31, 2016

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


-----------------------------------------------------------------------------------------------------
Update: For QGIS 3.x go to this page
-----------------------------------------------------------------------------------------------------



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.

-----------------------------------------------------------------------------------------------------
Update: For QGIS 3.x go to this page
-----------------------------------------------------------------------------------------------------

2 comments:

  1. Hi thanks for your guidance. However, I followed step 5) and received the error below.

    C:\Windows\System32>python ez_setup.py
    Downloading http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
    Traceback (most recent call last):
    File "ez_setup.py", line 278, in
    main(sys.argv[1:])
    File "ez_setup.py", line 210, in main
    egg = download_setuptools(version, delay=0)
    File "ez_setup.py", line 158, in download_setuptools
    src = urllib2.urlopen(url)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 410, in open
    response = meth(req, response)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 448, in error
    return self._call_chain(*args)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 403: SSL is required

    Have you got any idea how I can fix it? Many thanks!

    ReplyDelete
  2. Hi thanks for your guidance. However, I followed step 5) and received the error below.

    C:\Windows\System32>python ez_setup.py
    Downloading http://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg
    Traceback (most recent call last):
    File "ez_setup.py", line 278, in
    main(sys.argv[1:])
    File "ez_setup.py", line 210, in main
    egg = download_setuptools(version, delay=0)
    File "ez_setup.py", line 158, in download_setuptools
    src = urllib2.urlopen(url)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 410, in open
    response = meth(req, response)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 448, in error
    return self._call_chain(*args)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
    File "C:\PROGRA~1\QGIS2~1.18\apps\Python27\lib\urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
    urllib2.HTTPError: HTTP Error 403: SSL is required

    Have you got any idea how I can fix it? Many thanks!

    ReplyDelete