简体   繁体   English

在Python中导入PyQGIS模块时出现问题

[英]Problems while importing PyQGIS modules in Python

As my previous question has been closed, I'm trying to improve it to let the community understand it could be usefull for a broad number of users. 由于我的上一个问题已经结束,因此我正在尝试对其进行改进,以使社区了解它可能对广大用户有用。 I found a SHELL LAUNCHER that permit to bypass the system environment setting in the "traditional way" to integrate python and QGIS (namely using PyQGIS). 我找到了一个SHELL LAUNCHER,可以通过“传统方式”绕过系统环境设置,以集成python和QGIS(即使用PyQGIS)。 The original one works perfectly (find it at http://inasafe.linfiniti.com/html/id/developer-docs/platform_windows.html ). 原始版本可以完美运行(请在http://inasafe.linfiniti.com/html/id/developer-docs/platform_windows.html上找到)。 I've been trying to modify it to let anybody use his own favourite IDE (and not the windows command prompt as the original one is supposed to do). 我一直在尝试对其进行修改,以使任何人都可以使用自己喜欢的IDE(而不是Windows命令提示符,因为原始命令应该这样做)。 Therefore, I made my modifications and come eventually to this comand launcher: 因此,我进行了修改,最终使用了该命令启动器:

@echo off
SET OSGEO4W_ROOT=C:\QUANTU~1
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\Python27\Lib\idlelib\idle.pyw # this is the IDE I wanna use, but You can freely point to whatever You'd like
@echo off
SET GDAL_DRIVER_PATH=%IDLE%\bin\gdalplugins\1.9
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-6.4.2\lib
path %PATH%;"%OSGEO4W_ROOT%\apps\Python27\Scripts\"

set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python;
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis

start "Quantum GIS Shell" /B "cmd.exe" %*

Anyway, when I run it, it opens idle.pyw but I can't import the modules like if the system variables are still not set properly, although it seems they are by investigating the sys.path. 无论如何,当我运行它时,它会打开idle.pyw,但是我无法导入模块,就像系统变量仍未正确设置一样,尽管似乎它们是通过研究sys.path来完成的。 The error I get when I import (for instance) "qgis.core" module is: 导入(例如)“ qgis.core”模块时遇到的错误是:

ImportError: DLL load failed: Impossibile trovare il modulo specificato # (impossible to import the specified module)

My OS is Windows XP sp3, Python is 2.7.3 and QGIS is Lisboa v.1.8. 我的操作系统是Windows XP sp3,Python是2.7.3,QGIS是Lisboa v.1.8。 I hope someone could help me and that this could contribute to hsolve the common problem that is importing PyQGIS modules. 我希望有人能帮助我,这可能有助于解决导入PyQGIS模块的常见问题。

Eventually I managed to have the personal shell launcher! 最终,我设法拥有了个人外壳启动器! The trick was to investigate the o4w_env.bat file in order to understand what the "call" at the second line actually does. 诀窍是研究o4w_env.bat文件,以便了解第二行的“调用”实际上是做什么的。

So I tried and succeeded with this: 所以我尝试并成功了:

  1. Copy the idle.pyw (or the IDE you'd like to use to the Quantum GIS main folder (in my case it was C:/Quantum_GIS_Lisboa ) idle.pyw (或您要使用的IDE)复制到Quantum GIS主文件夹(在我的情况下为C:/Quantum_GIS_Lisboa

  2. Copy the SHELL LAUNCHER (see point 3) with extension .bat in "\\bin" 在“ \\ bin”中复制扩展名为.bat的SHELL LAUNCHER(请参阅第3点)

  3. Run this SHELL LAUNCHER (simple double click): 运行以下SHELL LAUNCHER(简单的双击):

     @echo off SET OSGEO4W_ROOT=C:\\QUANTU~1 call "%OSGEO4W_ROOT%"\\bin\\o4w_env.bat @echo off SET GDAL_DRIVER_PATH=%IDLE%\\bin\\gdalplugins\\1.9 path %PATH%;%OSGEO4W_ROOT%\\apps\\qgis\\bin path %PATH%;%OSGEO4W_ROOT%\\apps\\grass\\grass-6.4.2\\lib path %PATH%;"%OSGEO4W_ROOT%\\apps\\Python27\\Scripts\\" set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\\apps\\qgis\\python; set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\\apps\\Python27\\Lib\\site-packages set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\\apps\\qgis start "Quantum GIS Shell" /B "idle.pyw" %* # This is where you specify the IDE # you want to use, mine is "idle.pyw", but if You copy another one (as in point 1), # you should replace "idle.pyw" with "<YOUR_IDE>" 

Hope this could help anybody. 希望这可以对任何人有帮助。 Cheers! 干杯!

Run Qgis and open python console type the following 运行Qgis并打开python控制台,键入以下内容

  1. import sys 导入系统
  2. sys.path 系统路径

    Now copy this path and include the sys path at the top for your python code. 现在复制此路径,并在顶部为您的python代码添加sys路径。

Now run that code. 现在运行该代码。

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

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