# Working a PyQGIS Script in cmd, outside of the QGIS python console

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'

• Is there any reason you use pure cmd instead of OSGeo Shell (which is also cmd)? – Kadir Şahbaz Jan 6 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 Jan 6 at 12:17
• I've already tried. I get the same error in OSGeo Shell. – Kadir Şahbaz Jan 6 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 Jan 6 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 Jan 6 at 12:32

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