将QGIS模块导入Python(Anaconda)

3

在Anaconda的Spyder中控制QGIS。

我将PAYTHONPATH设置为C:\Program Files\QGIS Pisa\apps\qgis\bin,但仍然在导入qgis.core模块时出现此错误:

import qgis.core

ImportError: No module named qgis.core

如何导入模块?


2
希望这不是你的错误 PAYTHONPATH。 - Radu Ionescu
不需要,Spyder中已经有PYTHONPATH管理工具可以帮助选择所需的路径。我正在使用它。 - user1949719
1
希望你理解Radu的评论:你的环境变量P*A*ython中有一个拼写错误吗?如果不是这个错误,请确保qgis库在你的路径上,你可以通过仔细检查`>>> import sys
print(sys.path)`来进行检查。
- Oliver W.
3个回答

5

j08lue提供的答案对我有用。但我们也可以在Anaconda虚拟环境中以特定的环境范围方式完成此操作。因此,请尝试以下步骤:

  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.


1
这个可以运行。唯一的问题是我没有conda-develop,所以使用了“conda develop PASTEHERE -n conda-qgis”。 - Richard

3

QGIS中附带的Python包位于\path\to\QGIS\apps\Python27\Lib。因此,您需要将其添加到PYTHONPATH中,而不是...\qgis\bin

最好按脚本基础进行操作,而不是整个系统,像这样:

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

import qgis.core

请注意,QGIS Python软件包可能是为不同版本的Python构建的。因此,有些功能可能无法正常工作。

注意:QGIS Python插件安装在这里:~\.qgis2\python\plugins,因此您可能需要sys.path.append它。


我知道这已经超过2年了,但是QGIS 3.x版本附带了Python 3,因此现在可以选择在Python 3环境中使用QGIS模块。 - Dror Bogin
谢谢,@Dror,但那与这个问题没有真正的关系,是吗?Python 3已经通过OSGeo4W可用了很长时间。不幸的是,QGIS 3并没有使将QGIS / OSGeo4W Python环境链接到外部Anaconda环境变得更加方便。但是现在您可以通过Anaconda安装QGIS https://anaconda.org/conda-forge/qgis - j08lue

0

这是关于2023年和QGIS 3的内容:即使我没有尝试重用C:\Program Files下的安装,而是通过Anaconda(conda install -c conda-forge qgis;在我的情况下,这是Miniconda3)安装了一个处理副本,但我无法让import qgis正常工作。

起初,我只得到了一个ImportError;这是在我的普通Powershell环境下。然后我打开了Anaconda Powershell提示符,但在那里也不起作用。执行conda init powershell报告说没有要做的事情。我怀疑我PS环境中的其他调整可能会导致问题,所以我接着尝试了经典的(基于cmd.exe的)Anaconda提示符

它起作用了。然后我将值sys.path与其在Powershell下的值进行比较,发现对于某些原因,路径%CONDA_PREFIX%/Library/python在后者中丢失。最终我通过在%CONDA_PREFIX%/Lib/site-packages下创建一个.pth文件并将缺失的路径粘贴到该文件中来解决这个问题。这样,每次激活环境时都会自动添加到sys.path中,而我不必去修改%PYTHONPATH%;这是不推荐的,因为Anaconda不使用它,当我第一次尝试使用它时,它破坏了我的环境


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接