在Python中导入PyQGIS模块时出现问题(Problems while importing PyQGIS modules in Python)

编程入门 行业动态 更新时间:2024-10-26 08:31:05
在Python中导入PyQGIS模块时出现问题(Problems while importing PyQGIS modules in Python)

由于我之前的问题已经结束,我正在努力改进它,让社区了解它可能对广大用户有用。 我找到了一个SHELL LAUNCHER,允许以“传统方式”绕过系统环境设置来集成python和QGIS(即使用PyQGIS)。 原始的工作完美(在http://inasafe.linfiniti.com/html/id/developer-docs/platform_windows.html找到它)。 我一直在尝试修改它以让任何人使用他自己喜欢的IDE(而不是原始的IDE命令提示符)。 因此,我做了我的修改,最终来到这个命令发射器:

@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" %*

无论如何,当我运行它时,它会打开idle.pyw但是我无法导入模块,就好像系统变量仍未正确设置,尽管看起来它们是通过调查sys.path。 导入(例如)“qgis.core”模块时得到的错误是:

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

我的操作系统是Windows XP sp3,Python是2.7.3,QGIS是Lisboa v.1.8。 我希望有人可以帮助我,这有助于解决导入PyQGIS模块的常见问题。

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). The original one works perfectly (find it at 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). 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. The error I get when I import (for instance) "qgis.core" module is:

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. I hope someone could help me and that this could contribute to hsolve the common problem that is importing PyQGIS modules.

最满意答案

最终我设法拥有了个人炮弹发射器! 诀窍是调查o4w_env.bat文件,以便了解第二行的“调用”实际上是做什么的。

所以我尝试并成功了:

将idle.pyw (或您要使用的IDE)复制到Quantum GIS主文件夹(在我的例子中是C:/Quantum_GIS_Lisboa )

复制SHELL LAUNCHER(参见第3点),扩展名为.bat in“\ bin”

运行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>"

希望这可以帮助任何人。 干杯!

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.

So I tried and succeeded with this:

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)

Copy the SHELL LAUNCHER (see point 3) with extension .bat in "\bin"

Run this SHELL LAUNCHER (simple double click):

@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!

更多推荐

本文发布于:2023-08-06 08:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1446128.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模块   PyQGIS   Python   modules   importing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!