4

I keep getting this error in python from trying to call qgis from a script.

The code is:

from qgis.core import *
from qgis.analysis import *

I have read every posting on SO about this; wiped QGIS and reinstalled. Reset my PYTHON_PATH and QGIS_PREFIX variables to the correct directory. I've also checked the dependencies via dpkg -l | grep qgisand all of my dependencies are the xenial version.

Any other suggestions?

scdavis50
  • 111
  • 1
  • 5

2 Answers2

1

I had the same problem but it was with Windows 7. Following the last point called Running Custom Applications in http://docs.qgis.org/2.8/en/docs/pyqgis_developer_cookbook/intro.html I solved it.

You will need to tell your system where to search for QGIS libraries and appropriate Python modules if they are not in a well-known location — otherwise Python will complain:

>>> import qgis.core
ImportError: No module named qgis.core

This can be fixed by setting the PYTHONPATH environment variable. In the following commands, qgispath should be replaced with your actual QGIS installation path:

on Linux: export PYTHONPATH=/qgispath/share/qgis/python
on Windows: set PYTHONPATH=c:\qgispath\python

The path to the PyQGIS modules is now known, however they depend on qgis_core and qgis_gui libraries (the Python modules serve only as wrappers). Path to these libraries is typically unknown for the operating system, so you get an import error again (the message might vary depending on the system):

>>> import qgis.core
ImportError: libqgis_core.so.1.5.0: cannot open shared object file: No such file or directory

Fix this by adding the directories where the QGIS libraries reside to search path of the dynamic linker:

on Linux: export LD_LIBRARY_PATH=/qgispath/lib
on Windows: set PATH=C:\qgispath;%PATH%

These commands can be put into a bootstrap script that will take care of the startup. When deploying custom applications using PyQGIS, there are usually two possibilities:

require user to install QGIS on his platform prior to installing your application. The application installer should look for default locations of QGIS libraries and allow user to set the path if not found. This approach has the advantage of being simpler, however it requires user to do more steps. package QGIS together with your application. Releasing the application may be more challenging and the package will be larger, but the user will be saved from the burden of downloading and installing additional pieces of software. The two deployment models can be mixed - deploy standalone application on Windows and Mac OS X, for Linux leave the installation of QGIS up to user and his package manager.

CarlosHR
  • 11
  • 2
  • Carlos- Thanks for your response. I think I had read your earlier post re Win 7. I have my `PYTHON_PATH` variable set to `/usr/share/qgis/python` and the `LD_LIBRARY_PATH` variable set to `/usr/lib/qgis` and I still can't get it to import. Any other suggestions? – scdavis50 Sep 06 '16 at 01:38
  • BTW, I just noticed I had an underscore in `PYTHON_PATH`. I've removed the underscore so now it is `PYTHONPATH` but I am still getting the same result. – scdavis50 Sep 06 '16 at 02:17
0

Finally got it working. Had to completely wipe and reinstall QGIS twice and separately remove python-qgis. Also had to uninstall anaconda. After the second fresh install of QGIS I've gotten it working.

No other changes to my configuration.

scdavis50
  • 111
  • 1
  • 5