4

I have problems using version 3 of QGIS with Python 3. I followed different tutorials like this one : how to use pyqgis processing runalg clipvectorsbypolygon

But it seems to be working only with QGIS 2.XX and not 3.0. Here is what I try to run :

from qgis.core import *
import PyQt5.QtCore
import sys
sys.path.append('C:/QGIS 3.0/apps/qgis/python/plugins')
qgs = QgsApplication([], True)
qgs.setPrefixPath("C:/QGIS 3.0/apps/qgis", True)
qgs.initQgis()
import processing
qgs.exitQgis()

But I get on PyCharm the error message : "Process finished with exit code 1". I don't understand what is missing in my code. Can you please explain me how to use processing library with independent pyqgis scripts ?

asked May 29, 2018 at 15:23

2 Answers 2

5

I tested your code and stripped the problem down to the paths you are using. The error seems to be the forward slashes (/) that confuse your windows python installation. You should use backslashes on windows and use them as escaped symbol (eg. double backslash \). I updated your code with the paths of my system (an OSGeo4W64 installation), you can change them to match your directory structure:

from qgis.core import *
import PyQt5.QtCore
import sys
qgs = QgsApplication([], True)
qgs.setPrefixPath("C:\\OSGeo4W64\\apps\\qgis", True) 
#should be C:\\QGIS 3.0\\apps\\qgis
qgs.initQgis()
sys.path.append('C:\\OSGeo4W64\\apps\\qgis\\python\\plugins')
# should be "C:\\QGIS 3.0\\apps\\qgis\\python\\plugins" in your system
import processing
qgs.exitQgis()

Now the script should work. Sidenote: I ran the script from OSGeo4W Shell using the python-qgis command (python interpreter with QGIS paths). In case PyCharm still throws some errors, try running your script there or in PyDev (Eclipse). For reference: There is a similar answer to this one that also shows how to call native c++ algorithms from python.

answered May 29, 2018 at 21:23
1
  • I always have the same issue... Ok for the paths but I think my problem comes maybe from the Windows global path. Can you give me what you have in PC path concerning python and qgis ? Thank you for your help. Commented May 30, 2018 at 10:26
0

I solved my problem by adding the following line to my code :

sys.path.append("C:\\QGIS 3.0\\apps\\Python36\\Lib\\site-packages")

In fact when I tried to interpret my python script with PyCharm the problem was that I didn't get any information about my mistake. I ran it with the basic IDLE for Python 3 and it said me ModuleNotFoundError: No module named 'osgeo' which means the link to the library is not done because osgeo is installed with QGIS. The link to the library is what I wrote above.

BUT notice that you could also add this path to your %PYTHONPATH% variable. You mustn't do that because the folder site-packages\ contains many other libraries that can confuse Python executables like pip.

answered May 31, 2018 at 12:18

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.