Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import QGIS modules into Python (Anaconda)

Tags:

python

qgis

control QGIS from within Anaconda's Spyder.

I set PAYTHONPATH to C:\Program Files\QGIS Pisa\apps\qgis\bin, but it still gives this error while import qgis.core module:

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

How can I import the module?

like image 498
user1949719 Avatar asked Oct 16 '25 03:10

user1949719


2 Answers

The answer provided by j08lue works for me. But we also could do this in an Anaconda virtual environment in a specific environment-wide way. So, please try the following steps:

  1. Create a conda environment using conda create -n conda-qgis and then activate this new environment by using conda activate conda-qgis.

  2. Install QGIS through conda-forge in the current environment using conda install -c conda-forge qgis.

  3. Open QGIS by running qgis.

  4. Use the Python console in QGIS GUI, and run:

    import sys
    sys.path
    

    and you might get system paths like below:

    'C:/Anaconda3/envs/conda-qgis/Library/./python', 'C:/Users/Guohan/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python', 'C:/Users/Guohan/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python/plugins', 'C:/Anaconda3/envs/conda-qgis/Library/./python/plugins', 'C:\\Anaconda3\\envs\\conda-qgis\\Library\\python', 'C:\\Anaconda3\\envs\\conda-qgis\\Library\\python\\plugins', 'C:\\', 'C:\\Anaconda3\\envs\\conda-qgis\\python39.zip', 'C:\\Anaconda3\\envs\\conda-qgis\\DLLs', 'C:\\Anaconda3\\envs\\conda-qgis\\lib', 'C:\\Anaconda3\\envs\\conda-qgis\\Library\\bin', 'C:\\Anaconda3\\envs\\conda-qgis', 'C:\\Anaconda3\\envs\\conda-qgis\\lib\\site-packages', 'C:\\Anaconda3\\envs\\conda-qgis\\lib\\site-packages\\win32', 'C:\\Anaconda3\\envs\\conda-qgis\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda3\\envs\\conda-qgis\\lib\\site-packages\\Pythonwin', 'C:/Users/Guohan/AppData/Roaming/QGIS/QGIS3\\profiles\\default/python'
    
  5. Copy all the paths above and get back to the command prompt and run:

    conda-develop PASTEHERE -n conda-qgis
    

    This will create a conda.pth file at the site-package directory, which stores all the environment path variables specified for this conda-qgis environment.

  6. Finally, you should be able to use import qgis in an Anaconda environment.

like image 94
Guohan Zhao Avatar answered Oct 18 '25 19:10

Guohan Zhao


The Python packages shipped with QGIS live in \path\to\QGIS\apps\Python27\Lib. So you need to add that to PYTHONPATH, rather than ...\qgis\bin.

It is best to do this on script-basis, rather than system-wide, like so:

import sys
sys.path.append("C:\Program Files\QGIS Pisa\apps\Python27\Lib")

import qgis.core

But be aware that the QGIS Python packages were likely built for a different version of Python. So things might not work smoothly.

Note: QGIS Python plugins are installed here: ~\.qgis2\python\plugins, so you might need to sys.path.append that too.

like image 37
j08lue Avatar answered Oct 18 '25 18:10

j08lue



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!