[GIS] Access QGIS API from Anaconda python environment using Conda-Forge

anacondacondaqgis

I am trying to use QGIS in stand-alone python scripts (windows 10, python 3.6.6).

After the following steps…

1) create new conda environment (geospatial)

2) install the qgis conda-forge package through conda install -c conda-forge qgis as per conda-forge page (https://anaconda.org/conda-forge/qgis)

3) run anaconda prompt and activate the geospatial environment

… I run python and try to import qgis.core. However I get the "ModuleNotFoundError" error.

(geospatial) C:\Users\vince>python
Python 3.6.6 | packaged by conda-forge | (default, Jul 26 2018, 11:48:23) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import qgis.core
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'qgis'

From reading other resources, it seems PYTHONPATH and PATH need to be set, but after setting those (see below), I still get an error on import qgis.core, this time an "ImportError: DLL load failed" error.

See the following anaconda prompt output:

(geospatial) C:\Users\vince>set PYTHONPATH=C:\Anaconda3\envs\geospatial\Library\python

(geospatial) C:\Users\vince>set PATH=C:\Anaconda3\envs\geospatial\Library\bin;%PATH%

(geospatial) C:\Users\vince>python
Python 3.6.6 | packaged by conda-forge | (default, Jul 26 2018, 11:48:23) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import qgis.core
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda3\envs\geospatial\Library\python\qgis\__init__.py", line 72, in <module>
    from qgis.core import QgsFeature, QgsGeometry
  File "C:\Anaconda3\envs\geospatial\Library\python\qgis\core\__init__.py", line 34, in <module>
    from qgis._core import *
ImportError: DLL load failed: The specified module could not be found.

The (Python) paths I set above were "derived" from similar paths I found when one wants to use QGIS API by installing "osgeo4w. So, the main question: When using the Conda-Forge QGIS package, what are the required PYTHONPATHs and PATHs to be set; and/or what other configuration is needed to get the conda-forge QGIS package going?

Best Answer

If those steps are exactly what you did the problem may be that you installed the package in the wrong environment.

Prior to installing qgis (step 2) you have not activated the environment which means qgis is installed in you base environment. You are then trying to add the path to a library outside of the environment which can be tricky for an application like this.

Try activate 'geospatial' first and then install qgis.

Related Question