[英]import QGIS modules into Python (Anaconda)
control QGIS from within Anaconda's Spyder.从 Anaconda 的 Spyder 中控制 QGIS。
I set PAYTHONPATH
to C:\\Program Files\\QGIS Pisa\\apps\\qgis\\bin
, but it still gives this error while import qgis.core
module:我将
PAYTHONPATH
设置为C:\\Program Files\\QGIS Pisa\\apps\\qgis\\bin
,但在导入qgis.core
模块时仍然出现此错误:
import qgis.core
ImportError: No module named qgis.core
How can I import the module?如何导入模块?
The Python packages shipped with QGIS live in \\path\\to\\QGIS\\apps\\Python27\\Lib
. QGIS 附带的 Python 包位于
\\path\\to\\QGIS\\apps\\Python27\\Lib
。 So you need to add that to PYTHONPATH
, rather than ...\\qgis\\bin
.所以你需要将它添加到
PYTHONPATH
,而不是...\\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.但请注意,QGIS Python 包可能是为不同版本的 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.注意:QGIS Python 插件安装在这里:
~\\.qgis2\\python\\plugins
,所以你可能也需要sys.path.append
。
The answer provided by j08lue works for me. j08lue 提供的答案对我有用。 But we also could do this in an Anaconda virtual environment in a specific environment-wide way.
但我们也可以在 Anaconda 虚拟环境中以特定环境范围的方式执行此操作。 So, please try the following steps:
因此,请尝试以下步骤:
Create a conda environment using conda create -n conda-qgis
and then activate this new environment by using conda activate conda-qgis
.通过创建一个畅达环境
conda create -n conda-qgis
,然后激活使用这个新的环境conda activate conda-qgis
。
Install QGIS through conda-forge in the current environment using conda install -c conda-forge qgis
.使用
conda install -c conda-forge qgis
在当前环境中通过conda install -c conda-forge qgis
安装 QGIS。
Open QGIS by running qgis
.通过运行
qgis
打开 QGIS。
Use the Python console in QGIS GUI, and run:在 QGIS GUI 中使用 Python 控制台,然后运行:
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.这将在 site-package 目录中创建一个 conda.pth 文件,该文件存储为此 conda-qgis 环境指定的所有环境路径变量。
Finally, you should be able to use import qgis
in an Anaconda environment.最后,您应该能够在 Anaconda 环境中使用
import qgis
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.