Python 如何在Windows上正确设置PyQGIS环境?

Python 如何在Windows上正确设置PyQGIS环境?,python,import,module,cmd,qgis,Python,Import,Module,Cmd,Qgis,我有一个特定的错误,这是很常见的问题,从我所看到的,但似乎无法找到解决办法 当我尝试导入qgis模块时,出现以下错误: ImportError: No module named 'qgis' 到目前为止,这是我正在使用的,下面是.cmd的设置,我假设它与此有关: @echo off SET OSGEO4W_ROOT=D:\OSGeo4W64 call "%OSGEO4W_ROOT%"\bin\o4w_env.bat call "%OSGEO4W_ROOT%"\apps\grass\grass-

我有一个特定的错误,这是很常见的问题,从我所看到的,但似乎无法找到解决办法

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

ImportError: No module named 'qgis'
到目前为止,这是我正在使用的,下面是.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

我假设它必须使用调用Python3.4而不是Python2.7

编辑:第三轮

我这次的尝试包括对Lucas提案的一个稍加修改的版本

我所做的是删除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 2.7.5(默认,2013年5月15日,22:44:16)[MSC v.1500 64位(AMD64)]在win上
32
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入qgis.core
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“D:\OSGeo4W64\apps\qgis\python\qgis\\uuuuu init\uuuuu.py”,第36行,在
从qgis.core导入QgsFeature,QgsGeometry
文件“D:\OSGeo4W64\apps\qgis\python\qgis\core\\uuuuu init\uuuuu.py”,第3行,在
从qgis.\u核心导入*
ImportError:DLL加载失败:找不到指定的模块。
>>>

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


谢谢@luca76,它很好地加载了PyCharm,但没有解决问题,或者它确实加载了,但又出现了另一个问题。我尝试了这里找到的另一个解决方案。在我运行python脚本的最后一步之前,一切都很好,至少我相信这一点。我得到了与初始状态相同的错误导入。显然,太多的Python安装加上ArcGIS Python依赖使得事情变得非常复杂。这就是为什么我可能会为我的插件在Linux上工作,这似乎是一个无法克服的问题。你能用同样的答案尝试一下我修改过的脚本吗?我已经修改了环境变量的顺序,它甚至不会加载PyCharm。无论如何,谢谢你的努力@luca76。我在Linux上也遇到了问题,这次是编译文件。
@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" %*