[GIS] ImportError: DLL load failed: The specified procedure could not be found

pyqgis

When I try to load qgis within an external Python instance, I get the following error:

>>> from PyQt4.QtCore import *
>>> from PyQt4.QtGui import *
>>> import qgis
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "E:\QGIS\apps\qgis\python\qgis\__init__.py", line 27, in <module>
    from qgis.core import QgsFeature, QgsGeometry
  File "E:\QGIS\apps\qgis\python\qgis\core\__init__.py", line 30, in <module>
    from qgis._core import *
ImportError: DLL load failed: The specified procedure could not be found.

QGIS version: 2.16.1.

Python in QGIS:

>>> print(sys.version)
2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]

Python installation:

>>> print(sys.version)
2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)]

Paths in Python installation:

>>> print(sys.path)
['', 'E:\\QGIS\\apps\\qgis\\python', ... 

Additionally the following paths are set in the environment of Windows:

E:\QGIS\apps\qgis\python;E:\QGIS\apps\qgis\bin;

Any idea how to fix this? Or how shall I setup my system to be able to access QGIS "interactively" from external Python IDEs ?

Best Answer

For those still trying to make this work, here is my working configuration (with QGIS 2.14 LTR) to launch PyCharm with a fully working QGIS environment.

@echo off
call "C:\Program Files\QGIS 2.14\bin\o4w_env.bat"
@echo off
path %OSGEO4W_ROOT%\apps\qgis-ltr\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis-ltr
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis-ltr\qtplugins;%OSGEO4W_ROOT%\apps\qt4\plugins
set PYTHONPATH=%PYTHONPATH%;C:\Program Files\QGIS 2.14\apps\qgis-ltr\python;C:\Program Files\QGIS 2.14\apps\qgis-ltr\python\plugins
set PATH=%OSGEO4W_ROOT%\\apps\\qgis-ltr\\bin;%PATH%
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27
@"C:\Program Files (x86)\JetBrains\PyCharm 2016.2.2\bin\pycharm.exe"

Note that with the above configuration (saved as a .bat file to launch pycharm) it still misses something in the IDE to get syntax completion for the QGIS API.