11

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 ran python and tried to import qgis.core. And I got 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?

3
  • 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
    Commented Jan 30, 2019 at 1:59
  • 4
    @VincentV Hello. Have you found the answer yet? Your question is where many of us are stuck!
    – Ash
    Commented Sep 29, 2019 at 15:46
  • link to the great Underdark new post : anitagraser.com/2023/01/21/… Commented Jan 22, 2023 at 7:57

6 Answers 6

5

I can't speak to what was happening in 2019 when this question was opened. In 2023, the answer is you don't have to do anything extra after installing qgis.

I just tested on 32-bit Windows 10 Home and 64-bit Windows 11 Home using both existing and brand new user accounts, and the original steps taken by the OP work without any modifications needed.

The following steps should work:

  1. Open an 'Anaconda Prompt'
  2. Create a new conda environment: geospatial
    • conda create -n geospatial
  3. Activate geospatial conda environment
    • conda activate geospatial
  4. Install QGIS package through conda-forge, as per Qgis :: Anaconda.org
    • conda install -c conda-forge qgis
  5. Launch Python session
    • python
  6. Import QGIS
    • import qgis

All of the environment variables and paths that earlier answers talk about are taken care of now by the QGIS package installation through conda-forge. Specifically, the conda-forge QGIS package places several new files into the activation folder for the conda environment:

(base) C:\>dir /b %CONDA_PREFIX%\envs\geospatial\etc\conda\activate.d
gdal-activate.bat
gdal-activate.ps1
gdal-activate.sh
geotiff-activate.bat
geotiff-activate.sh
pdal-activate.bat
pdal-activate.sh
proj4-activate.bat
proj4-activate.sh
qgis-activate.bat
qgis-activate.sh

These batch files and shell scripts modify and add environment variables to make importing QGIS run. In addition to the typical changes made by conda when activating an environment, the following environment variables are set by the QGIS conda activation scripts:

GDAL_DATA=%CONDA_PREFIX%3\envs\geospatial\Library\share\gdal
GDAL_DRIVER_PATH=%CONDA_PREFIX%3\envs\geospatial\Library\lib\gdalplugins
GEOTIFF_CSV=%CONDA_PREFIX%3\envs\geospatial\Library\share\epsg_csv
PDAL_DRIVER_PATH=%CONDA_PREFIX%3\envs\geospatial\Library\bin
PROJ_DATA=%CONDA_PREFIX%3\envs\geospatial\Library\share\proj
PYTHONPATH=%CONDA_PREFIX%3\envs\geospatial\Library\python\plugins;%CONDA_PREFIX%3\envs\geospatial\Library\python;
QGIS_PREFIX_PATH=%CONDA_PREFIX%/envs/geospatial/Library
QT_PLUGIN_PATH=%CONDA_PREFIX%3\envs\geospatial\Library\qtplugins;%CONDA_PREFIX%3\envs\geospatial\Library\plugins;

If the steps above don't work, it means something didn't go correctly during the package installation, or there are multiple Python deployments on the same machine that are creating conflicting settings either with environment variables or registry settings.

3

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.

2
+200

It is the problem of your environment path variable, and you have to specify the correct path to find the DLL library of QGIS. Please try to follow the steps illustrated in this link below to get all environment path variables correctly specified:

https://stackoverflow.com/questions/35622661/import-qgis-modules-into-python-anaconda/67447061#67447061

1

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.

1

I think that the problem doesn't come from the PYTHONPATH or the PATH but from the fact that the _core file has the extension .pyd. When you rename it _core.py it finds it but you still get an error because pyd aren't the same thing as py.

For the moment the only way I found to work around this is to use the python library that is inside the qgis installation.

0

This is on 2023. I had a similar problem in Windows under Anaconda Powershell Prompt, but discovered that things do work under the cmd.exe-based Anaconda Prompt. Thus, I printed sys.path under that prompt and compared it to the sys.path under Anaconda Powershell Prompt. Then, I created a .pth file under %CONDA_PREFIX%/Lib/site-packages and pasted the missing paths – in my case missing %CONDA_PREFIX%/Library/python was the main culprit – into that file. That enabled me to import qgis after reactivating the environment.

There is a similar question at Stack Overflow.

I'd also recommend against modifying %PYTHONPATH%, because I tried that at first and it broke my active conda environment, disabling the conda commands; it's worth noting that Anaconda itself does not set the %PYTHONPATH% variable at all. I then had to hop through a few loops to rectify the situation (more details about that in this other answer of mine at Stack Overflow).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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