3

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?

Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
user1949719
  • 53
  • 1
  • 1
  • 6
  • 2
    I hope this is not your error **PAYTHON**PATH – Radu Ionescu Feb 25 '16 at 09:15
  • No, there is already PYTHONPATH manager tool in Spyder which helps to select the desired path. I use it. – user1949719 Feb 25 '16 at 09:26
  • 1
    I hope you understood Radu's comment: there's a typo in your environment variable `P*A*ython`? If that's not the error, ensure the qgis library is on your path, which you can check by carefully examining `>>> import sys >>> print(sys.path)` – Oliver W. Feb 25 '16 at 11:05

3 Answers3

5

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.

Shaido
  • 27,497
  • 23
  • 70
  • 73
Guohan Zhao
  • 171
  • 1
  • 1
  • 1
    This works. The only thing is I don't have conda-develop, so used "conda develop PASTEHERE -n conda-qgis" – Richard Oct 15 '21 at 17:13
3

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.

j08lue
  • 1,647
  • 2
  • 21
  • 37
  • I know this is more than 2yrs old but QGIS 3.x ships with python 3. so it is now optional to work with qgis modules in a python 3 environment. – Dror Bogin Oct 21 '18 at 13:23
  • Thanks, @Dror, but that is not really related to this question, is it? And Python 3 has been available via OSGeo4W for a long time. QGIS 3 unfortunately does not make it less awkward to link the QGIS / OSGeo4W Python environment to an external Anaconda one. But you can now install QGIS via Anaconda https://anaconda.org/conda-forge/qgis – j08lue Oct 22 '18 at 18:12
0

This is on 2023 and QGIS 3: I couldn't get import qgis to work even though I'm not trying to re-use the installation under C:\Program Files, but installed a processing copy via Anaconda (conda install -c conda-forge qgis; in my case, this is Miniconda3).

Initially, I just got an ImportError; this was under my normal Powershell environment. I then opened the Anaconda Powershell Prompt, but it didn't work in that either. Executing conda init powershell reported that there was nothing to do. I suspected that other tweaks in my PS environment might cause problems, so I then tried the classic (cmd.exe-based) Anaconda Prompt.

It worked. I then compared the value sys.path to the one it had under Powershell and realised that, for some reason, the path %CONDA_PREFIX%/Library/python was missing from the latter. I ultimately fixed this by creating a .pth file under %CONDA_PREFIX%/Lib/site-packages and pasting the missing path into that file. That way it will automatically get appended the sys.path every time the environment is activated, and I didn't have to mess with %PYTHONPATH%; it's not recommended, because Anaconda does not use it and when I first tried to use it, it broke my environment.