2

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?

  • Welcome to GIS SE. As a new user, please take the Tour, which emphasizes the importance of asking One question per Question. Please Edit your Question to ask one question. – Vince Jan 30 at 1:59
0

Hopefully somebody who understands the internal processes better than me can provide an answer.

You can add GDAL to your Anaconda installation, and use it this way for example:

import gdal
import ogr   # it should also be available if gdal is present

And then use these packages in your standalone script, which reproduces the internal processing of QGIS. For example:

srs = ogr.osr.SpatialReference()  # to define a spatial reference

geom = ogr.CreateGeometryFromWkt(MultiLineString(variable1).to_wkt()) # to work with geometries

I'm not sure if you can call the full QGIS environment in Python without runnning the QGIS app, since many items (for example, different GUI components) may be called and created when the environment starts up. If they cannot be created (because the GUI didn't start up), errors may occur.

Again, one of the developers can confirm this or teach us how to make it work.

Side note: Another very good way to handle spatial data in Python is using geopandas (for vector data) and rasterio (for raster data). They work well together too, without complications. Feel free to try them out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.