简体   繁体   English

导入错误:没有名为 qgis.core ubuntu 16.04 python 2.7 qgis 2.16.2 的模块

[英]ImportError: No module named qgis.core ubuntu 16.04 python 2.7 qgis 2.16.2

I keep getting this error in python from trying to call qgis from a script.我在尝试从脚本中调用 qgis 时一直在 python 中收到此错误。

The code is:代码是:

from qgis.core import *
from qgis.analysis import *

I have read every posting on SO about this;我已经阅读了关于 SO 的所有帖子; wiped QGIS and reinstalled.擦除QGIS并重新安装。 Reset my PYTHON_PATH and QGIS_PREFIX variables to the correct directory.将我的 PYTHON_PATH 和 QGIS_PREFIX 变量重置到正确的目录。 I've also checked the dependencies via dpkg -l | grep qgis我还通过dpkg -l | grep qgis检查了依赖项dpkg -l | grep qgis and all of my dependencies are the xenial version. dpkg -l | grep qgis和我所有的依赖项都是 xenial 版本。

Any other suggestions?还有其他建议吗?

I had the same problem but it was with Windows 7. Following the last point called Running Custom Applications in http://docs.qgis.org/2.8/en/docs/pyqgis_developer_cookbook/intro.html I solved it.我遇到了同样的问题,但它是在 Windows 7 上。按照http://docs.qgis.org/2.8/en/docs/pyqgis_developer_cookbook/intro.html 中名为 Running Custom Applications 的最后一点,我解决了它。

You will need to tell your system where to search for QGIS libraries and appropriate Python modules if they are not in a well-known location — otherwise Python will complain:如果 QGIS 库和合适的 Python 模块不在一个众所周知的位置,你需要告诉你的系统在哪里搜索它们——否则 Python 会抱怨:

>>> import qgis.core
ImportError: No module named qgis.core

This can be fixed by setting the PYTHONPATH environment variable.这可以通过设置 PYTHONPATH 环境变量来解决。 In the following commands, qgispath should be replaced with your actual QGIS installation path:在以下命令中,应将 qgispath 替换为您实际的 QGIS 安装路径:

on Linux: export PYTHONPATH=/qgispath/share/qgis/python在 Linux 上:export PYTHONPATH=/qgispath/share/qgis/python
on Windows: set PYTHONPATH=c:\\qgispath\\python在 Windows 上:设置PYTHONPATH=c:\\qgispath\\python

The path to the PyQGIS modules is now known, however they depend on qgis_core and qgis_gui libraries (the Python modules serve only as wrappers). PyQGIS 模块的路径现在是已知的,但是它们依赖于 qgis_core 和 qgis_gui 库(Python 模块仅用作包装器)。 Path to these libraries is typically unknown for the operating system, so you get an import error again (the message might vary depending on the system):这些库的路径对于操作系统来说通常是未知的,因此您再次收到导入错误(消息可能因系统而异):

>>> import qgis.core
ImportError: libqgis_core.so.1.5.0: cannot open shared object file: No such file or directory

Fix this by adding the directories where the QGIS libraries reside to search path of the dynamic linker:通过将 QGIS 库所在的目录添加到动态链接器的搜索路径来解决此问题:

on Linux: export LD_LIBRARY_PATH=/qgispath/lib在 Linux 上:导出LD_LIBRARY_PATH=/qgispath/lib
on Windows: set PATH=C:\\qgispath;%PATH%在 Windows 上:设置PATH=C:\\qgispath;%PATH%

These commands can be put into a bootstrap script that will take care of the startup.这些命令可以放入一个引导脚本中,该脚本将负责启动。 When deploying custom applications using PyQGIS, there are usually two possibilities:使用 PyQGIS 部署自定义应用程序时,通常有两种可能:

require user to install QGIS on his platform prior to installing your application.要求用户在安装应用程序之前在其平台上安装 QGIS。 The application installer should look for default locations of QGIS libraries and allow user to set the path if not found.应用程序安装程序应查找 QGIS 库的默认位置,如果未找到,则允许用户设置路径。 This approach has the advantage of being simpler, however it requires user to do more steps.这种方法的优点是更简单,但它需要用户做更多的步骤。 package QGIS together with your application.将 QGIS 与您的应用程序打包在一起。 Releasing the application may be more challenging and the package will be larger, but the user will be saved from the burden of downloading and installing additional pieces of software.发布应用程序可能更具挑战性并且包会更大,但用户将免于下载和安装其他软件的负担。 The two deployment models can be mixed - deploy standalone application on Windows and Mac OS X, for Linux leave the installation of QGIS up to user and his package manager.这两种部署模型可以混合使用 - 在 Windows 和 Mac OS X 上部署独立应用程序,对于 Linux,将 QGIS 的安装留给用户和他的包管理器。

Finally got it working.终于让它工作了。 Had to completely wipe and reinstall QGIS twice and separately remove python-qgis.必须完全擦除并重新安装 QGIS 两次并分别删除 python-qgis。 Also had to uninstall anaconda.还得卸载anaconda。 After the second fresh install of QGIS I've gotten it working.在第二次全新安装 QGIS 后,我已经开始工作了。

No other changes to my configuration.我的配置没有其他更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM