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-qgisand then activate this new environment by using- conda 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 qgisin 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.
Post a Comment for "Import Qgis Modules Into Python (anaconda)"