2

I am trying to run a standalone application. However, it does not run the program because of mentioned error in title. Here is my code:

from qgis.core import *
from qgis.gui import *
from qgis.utils import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

app = QgsApplication([], True)
path = "C:/OSGeo4W64/apps/qgis-dev"
app.setPrefixPath(path, True)
app.initQgis()

canvas = QgsMapCanvas()
title = "PyQGIS Standalone Application Example"
canvas.setWindowTitle(title)
canvas.setCanvasColor(Qt.white)

layer_info = 'LineString?crs=epsg:4326'
layer = QgsVectorLayer(layer_info, 'MyLine', "memory")
pr = layer.dataProvider()
linstr = QgsFeature()
wkt = "LINESTRING (1 1, 10 15, 40 35)"
geom = QgsGeometry.fromWkt(wkt)
linstr.setGeometry(geom)
pr.addFeatures([linstr])
layer.updateExtents()
QgsProject.instance().addMapLayer(layer)

canvas.setExtent(layer.extent())
canvas.setLayers([layer])
canvas.zoomToFullExtent()
exitcode = app.exec_()
QgsApplication.exitQgis()
sys.exit(exitcode)

When I run it, I get this error:

Traceback (most recent call last):
  File "C:/Users/DELL/Desktop/MyMap/myMap.py", line 1, in <module>
    from qgis.core import *
  File "C:\OSGeo4W64\apps\qgis-dev\python\qgis\__init__.py", line 72, in <module>
    from qgis.core import QgsFeature, QgsGeometry
  File "C:\OSGeo4W64\apps\qgis-dev\python\qgis\core\__init__.py", line 34, in <module>
    from qgis._core import *

ImportError: DLL load failed: The specified module could not be found.

Python version is: 3.6 PyQt version is: PyQt5.9 Windows 10 - 64bit

I also add many path:

%SystemRoot%\system32
%SystemRoot%
%SystemRoot%\System32\Wbem
%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\
C:\OSGeo4W64
C:\OSGeo4W64\apps
C:\OSGeo4W64\apps\gdal-dev
C:\OSGeo4W64\apps\gdal-dev\bin
C:\OSGeo4W64\apps\gdal-dev\pymod3\Lib\site-packages
C:\OSGeo4W64\apps\grass\grass-7.2.2\bin
C:\OSGeo4W64\apps\Python36
C:\OSGeo4W64\apps\Python36\Scripts
C:\OSGeo4W64\apps\Python36\DLLs
C:\OSGeo4W64\apps\Python36\lib\site-packages
C:\OSGeo4W64\apps\qgis-dev
C:\OSGeo4W64\apps\qgis-dev\bin
C:\OSGeo4W64\apps\qgis-dev\lib
C:\OSGeo4W64\apps\qgis-dev\python
C:\OSGeo4W64\apps\qgis-dev\python\qgis
C:\OSGeo4W64\apps\Qt5
C:\OSGeo4W64\apps\Qt5\lib
C:\OSGeo4W64\bin
C:\OSGeo4W64\apps\qgis-dev\python
C:\OSGeo4W64\apps\Python36\lib
C:\OSGeo4W64\include
C:\Users\Mustafa Uçar\AppData\Roaming\QGIS\QGIS3\profiles\default
C:\Users\Mustafa Uçar\AppData\Roaming\QGIS\QGIS3\profiles\default\QGIS

How can I fix this error?

8
  • 1
    Does that help? path = "C:/OSGeo4W64/apps/qgis-dev" Oct 20, 2017 at 12:02
  • @MatthiasKuhn I added all paths that I can add. You can see them in the edited question. Oct 20, 2017 at 14:22
  • Yes, I'm not sure what's wrong. This message normally is a hint that some dlls for python are missing (rather than an error in the script). I'm not very familiar with the setup on Windows, but extrapolating from Linux: There should be a _core.dll file in C:\OSGeo4W64\apps\qgis-dev\python\qgis which is either missing or corrupted. Oct 20, 2017 at 14:28
  • @MatthiasKuhn Yes there is a _core named file but it is not .dll. It is "_core.pyd" file in C:\OSGeo4W64\apps\qgis-dev\python\qgis Oct 20, 2017 at 14:48
  • That's not what you are looking for. Can you reinstall/upgrade the qgis-dev-python (might be called slightly different) package in osgeo4w? Oct 20, 2017 at 15:22

1 Answer 1

2

I have been working for 3 weeks for this problem. However, I found it finally. I write python console in qgis:

import sys
print (sys.path)

It gives the path and I paste them into PYTHONPATH and PATH. That's it!

5
  • Could you update your answer by posting the qgis-specific content of your PATH and PYTHONPATH variable maintaining your order of the paths? Unfortunately your approach doesn't work for me.
    – root676
    Nov 28, 2017 at 20:59
  • Can you try this please?PYTHONPATH=C:\OSGeo4W64\apps\Python36\lib\site-packages;C:\OSGEO4~1\apps\Python36\lib;C:\OSGeo4W64\apps\qgis-dev\python; PATH=C:\OSGeo4W64\apps\qgis-dev\bin;C:\OSGeo4W64\apps\Qt5\bin;C:\OSGeo4W64\apps\qgis-dev\python\plugins\processing; Nov 29, 2017 at 8:30
  • If the problem continues, let me know Nov 29, 2017 at 8:31
  • Thanks for posting the paths but the problem still exists in pydev - eclipse. Despite adding PyQt5 and qgis as forced builtins python throws me the error mentioned in the title of your question. I can't even add/configure the interpreter with your configuration...
    – root676
    Nov 29, 2017 at 19:51
  • ibb.co/gERiCw here is my variables you can use it. ıf error continues please tell me again Nov 30, 2017 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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