import QGIS modules into Python (Anaconda)
Solution 1
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:
Create a conda environment using
conda create -n conda-qgis
and then activate this new environment by usingconda activate conda-qgis
.Install QGIS through conda-forge in the current environment using
conda install -c conda-forge qgis
.Open QGIS by running
qgis
.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'
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.
Finally, you should be able to use
import qgis
in an Anaconda environment.
Solution 2
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.
user1949719
Updated on October 11, 2022Comments
-
user1949719 over 1 year
control QGIS from within Anaconda's Spyder.
I set
PAYTHONPATH
toC:\Program Files\QGIS Pisa\apps\qgis\bin
, but it still gives this error while importqgis.core
module:import qgis.core
ImportError: No module named qgis.core
How can I import the module?
-
Dror Bogin over 5 yearsI 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.
-
j08lue over 5 yearsThanks, @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 anaconda.org/conda-forge/qgis
-
Richard over 2 yearsThis works. The only thing is I don't have conda-develop, so used "conda develop PASTEHERE -n conda-qgis"