【问题标题】:How do I set the PyQGIS environnement on Windows properly?如何在 Windows 上正确设置 PyQGIS 环境?
【发布时间】:2018-06-07 18:00:12
【问题描述】:

我遇到了一个特定错误的问题,这在我所见的情况下很常见,但似乎找不到解决方案。

当我尝试导入 qgis 模块时出现以下错误:

ImportError: No module named 'qgis'

到目前为止,这是我正在使用的guide,这是 .cmd 的设置,我假设它与此有关:

@echo off
SET OSGEO4W_ROOT=D:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass-6.4.3\etc\env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib

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
set PATH=C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\bin\pycharm.exe;%PATH%
cd %HOMEPATH%\TER\development
start "PyCharm aware of Quantum GIS" /B "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\bin\pycharm.exe" %*

编辑:应用 luca76 的建议后

C:\Python34\python.exe D:/TER/development/test.py
Fatal Python error: Py_Initialize: unable to load the file system codec
  File "D:\OSGEO4~1\apps\Python27\lib\encodings\__init__.py", line 123
    raise CodecRegistryError,\
                            ^
SyntaxError: invalid syntax

进程以退出代码 3 结束

我假设它必须使用调用 Python 3.4 而不是 Python 2.7。

编辑:第三轮

这次我的尝试包括对卢卡斯提案的略微修改版本。

我所做的是删除 HOMEPATH 但是这次当我尝试导入 qgis 模块时,我得到了一个回溯:

Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import qgis.core
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\OSGeo4W64\apps\qgis\python\qgis\__init__.py", line 36, in <module>
    from qgis.core import QgsFeature, QgsGeometry
  File "D:\OSGeo4W64\apps\qgis\python\qgis\core\__init__.py", line 3, in <module
>
    from qgis._core import *
ImportError: DLL load failed: The specified module could not be found.
>>>

【问题讨论】:

    标签: python import module cmd qgis


    【解决方案1】:

    尝试在执行 o4w_env.bat 之前放置环境变量:(请记住修复所有目录路径!)

    @echo off
    SET OSGEO4W_ROOT=D:\OSGeo4W64
    set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%
    set PYTHONPATH=%OSGEO4W_ROOT%\apps\Python27\Lib\site-packages;%PYTHONPATH%
    set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%\apps\qgis
    set PATH=C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\bin\pycharm.exe;%PATH%
    path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
    path %OSGEO4W_ROOT%\apps\grass\grass-6.4.3\lib;%PATH%
    
    call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
    call "%OSGEO4W_ROOT%"\apps\grass\grass-6.4.3\etc\env.bat
    
    cd %HOMEPATH%\TER\development
    start "PyCharm aware of Quantum GIS" /B "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 4.0.6\bin\pycharm.exe" %*
    

    【讨论】:

    • 谢谢@luca76,它很好地加载了PyCharm,但没有解决问题,或者它确实解决了另一个问题。
    • 我尝试了另一种解决方案,在这里找到link。一切正常,至少我是这么认为的,直到我运行 python 脚本的最后一步。我得到与初始状态相同的错误导入。
    • 显然,太多的 Python 安装加上 ArcGIS Python 依赖使事情变得非常复杂。这就是为什么我可能会为我的插件在 Linux 上工作,这似乎是一个无法克服的问题。
    • 你能在同一个答案中尝试我修改过的脚本吗?我已经修改了环境变量的顺序
    • 它甚至不会加载 PyCharm。无论如何,感谢@luca76 的努力。我在 Linux 上也遇到了问题,这次是编译文件。
    猜你喜欢
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2010-10-31
    • 2014-07-09
    相关资源
    最近更新 更多