0

I try to run the QSwat script and it begins with that:

from PyQt4.QtCore import * # @UnusedWildImport
from PyQt4.QtGui import * # @UnusedWildImport
from qgis.core import * # @UnusedWildImport
from qgis.gui import * # @UnusedWildImport

However I get this error:

Traceback (most recent call last):
  File "C:/Program Files/QGIS 2.18/apps/qgis/python/plugins/HidroTurk/HT_HJK/delineation.py", line 25, in <module>
    from qgis.core import * # @UnusedWildImport
  File "C:\Program Files\QGIS 2.18\apps\qgis\python\qgis\__init__.py", line 26, in <module>
    from qgis.PyQt import QtCore
  File "C:\Program Files\QGIS 2.18\apps\qgis\python\qgis\PyQt\QtCore.py", line 28, in <module>
    sip.setapi(api, 2)
ValueError: API 'QDate' has already been set to version 1

I read that if I import qgis.core and qgis.gui later, it fixes the problem so I wrote like that:

from qgis.core import * # @UnusedWildImport
from qgis.gui import * # @UnusedWildImport
from PyQt4.QtCore import * # @UnusedWildImport
from PyQt4.QtGui import * # @UnusedWildImport

I got this error:

Traceback (most recent call last):
  File "C:/Program Files/QGIS 2.18/apps/qgis/python/plugins/HidroTurk/HT_HJK/delineation.py", line 23, in <module>
    from qgis.core import * # @UnusedWildImport
  File "C:\Program Files\QGIS 2.18\apps\qgis\python\qgis\__init__.py", line 27, in <module>
    from qgis.core import QgsFeature, QgsGeometry
  File "C:\Program Files\QGIS 2.18\apps\qgis\python\qgis\core\__init__.py", line 30, in <module>
    from qgis._core import *
ImportError: DLL load failed: The specified module could not be found

I am sure that the problem source is PATH, PYTHONPATH and PYTHONHOME. Because I got this error in QGIS 3 and I solved it by adding paths that can be reached by writing python console print (sys.path) to the system variables of PATH and PYTHONPATH.

However I can not fix it with the same way. I have been handling with it for 5 days and I will go crazy!

Note: I can import them in QGIS 2.18 python console.

1

I'm using a batchfile to set the environment for python and qgis, like this:

SET QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
SET PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python27
SET PATH=%PATH%;%OSGEO4W_ROOT%\apps\Python27\Scripts;%OSGEO4W_ROOT%\apps\qgis\bin

I put this batch file into the directory %OSGEO4W_ROOT%\etc\ini which is autmatically called by the OSGeo4W-Shell, or you can start mostly any Python-IDE by batch file and call that ini-batch file (here for pyscripter):

set PYSC_DIR=%~dp0

for %%F in ("%OSGEO4W_ROOT%\etc\ini\*.bat") do call "%%F"

SET PYTHONPATH=%PYTHONPATH%;%PYSC_DIR%;%PYSC_DIR%\lib;
SET PATH=%PYSC_DIR%;%PATH%

start "PyScripter" %PYSC_DIR%\PyScripter.exe --python27 --pythondllpath=%OSGEO4W_ROOT%\bin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.