3

I have a perfectly fine working script that runs within the QGIS Python Console and I would like to run the script outside of the software, preferably in cmd as a .bat, so then I can use task scheduler to run the task every minute. I am using QGIS downloaded with OSGEO4W64. My .bat looks like this and runs, until it encounters a variable within QGIS e.g. QGSRasterLayer or QgsPrintLayout:

ECHO OFF
cd C:\Path\to\.py Script
C:\OSGeo4W64\apps\Python37\python.exe .\Full_Script.py
PAUSE

NameError: name 'QgsRasterLayer' is not defined

I know I have to do from qgis.core import QgsRasterLayer but every time I try to use this piece of code, it says that 'qgis' is not defined. I have looked into many forums on this site and have looked mainly into path and PYTHONPATH environment variables. My path looks like this:

C:\OSGeo4W\apps\Python37
C:\OSGeo4W\apps\Python37\lib
C:\OSGeo4W\apps\Python37\Scripts
C:\OSGeo4W\bin\qgis-bin.exe

And my PYTHONPATH looks like this:

c:\OSGeo4W64\bin\python

I have also tried using sys.path.append but I have had no luck so far. Can anyone help me out so I can sort out this problem and finally get my script working outside of QGIS, as I am struggling to find a way how to resolve this issue? Have I inputted the paths wrong or do I need to do something else, in order so that I can import qgis.core and be able to use all the elements (QgsRasterLayer etc) in my script, outside of QGIS.

EDIT: I currently have it working now but it comes up with the error: NoneType object has no attribute 'RasterLayer'

12
  • Is there any reason you use pure cmd instead of OSGeo Shell (which is also cmd)? Commented Jan 6, 2021 at 12:11
  • Hi Kadir, no reason in particular. I just thought that the easiest way would be through cmd. If it easier, I can use OSGeo Shell if you could help me getting it working through that? Thanks
    – LiamHems
    Commented Jan 6, 2021 at 12:17
  • I've already tried. I get the same error in OSGeo Shell. Commented Jan 6, 2021 at 12:18
  • Ah that is annoying... I have been trying to get it working for a while but unfortunately I have not got anywhere with it yet...
    – LiamHems
    Commented Jan 6, 2021 at 12:21
  • please do not post pictures of your code or environment variables - it is impossible for some users to read (and just hard for the rest of us).
    – Ian Turton
    Commented Jan 6, 2021 at 12:32

1 Answer 1

4

If you want to run a pyqgis script - my_pyqgis_script.py in the example below - from outside, for example from a bat file, you should use the following syntax within your bat file:

"C:\Program Files\QGIS 3.10\bin\python-qgis.bat" my_pyqgis_script.py

you have to adjust the path to python-qgis.bat, in your case something like C:\OSGeo4W\qgis\bin just look for the python-qgis.bat file. In your python script you have to load all libraries like you described above (from qgis.core import QgsRasterLayer and so on). The python-qgis.bat will set all relevant path information and finally call you script.

5
  • Thank you for your answer. Within the script, I have added all the libraries e.g. from qgis.utils import iface from PyQt5.QtGui import QFont, QColor etc. However, when I go to the .bat file, I double click it and the command window comes up but then nothing happens after that. When I try to run the actual script, the message "Could not find the Qt platform plugin "windows" in "" comes up. Any ideas?
    – LiamHems
    Commented Jan 6, 2021 at 16:52
  • Since I don't know your PyQGIS script it is difficult to say what is happening. It sounds that you are trying to use GUI elements? Normally if you want to start a QGIS Process from outside, let's say to print a layout or make some processing, you woulnd't use iface for that - with the program line: app = QgsApplication([], false). On the other side, there can be other programs on your programm using QT, which makes it more difficult, you can check with cmd and type echo %QT_PLUGIN_PATH%. This should be empty, if not delete it and try again.
    – eurojam
    Commented Jan 7, 2021 at 7:06
  • Hi, I have managed to find and locate the Qt platform plugin using os.environ[QT_QPA_PLATFORM_PLUGIN_PATH] and os.environ[PATH] but now it comes up with the error - NoneType object has no attribute 'RasterLayer'. I am guessing it is because I am using iface.addRasterLayer or iface.addVectorLayer. What would be the best way to add these in without using iface?
    – LiamHems
    Commented Jan 7, 2021 at 10:20
  • 1
    If you want to do something with the vectorlayer or rasterlayer in background there is no need to add it to a mapcanvas. for example: layer = QgsVectorLayer('D:/buildings_a.shp',u'OSM Gebäude Freiburg', 'ogr') will load a vectorlayer into the variable layer. From that point you can do anything with layer like reproject, export, add features and so on, no need to add it...
    – eurojam
    Commented Jan 7, 2021 at 12:30
  • Thanks @eurojam for your answer. The simplest solution. Commented Oct 1, 2021 at 4:17

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.