0

I have tried adding sys.path.append and under cmd.exe set Python Path but am still getting the dreaded ImportError: No module named qgis.core.

I set PythonPath to C:\OSGeo4W64\apps\qgis\python

I have referenced sys.path.append material on this forum and on the QGIS site and I am still getting the error.

I am using IDLE to run scripts.

Can you provide any guidance?

Here is my Script to round raster cell values:

# Description: Converts each cell value of a raster to an integer by proper rounding
# Import required modules
# Python modules

import sys
sys.path.append("C:\OSGeo4W64\apps\qgis\python")
sys.path.append("C:\OSGeo4W64\apps\Python37\lib\site-packages")
sys.path.append("C:\OSGeo4W64\apps\qgis\python\qgis")

import time
import os
# Qgis modules
from qgis.core import *
import qgis.utils
from PyQt5.QtCore import QFileInfo, QSettings

import subprocess

subprocess.check_call([
"gdal_translate", "-of", "GTiff", "-ot", "Int32", 
"H:/test/demtest1.tif", "H:/test/output/int.tif"
])

#done
print("done")
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Todd
  • 45
  • 4
  • If you try print("C:\OSGeo4W64\apps\qgis\python"), you will see that you've forgotten to either escape the backslash characters (by doubling them) or use unix-style right slashes. However, I tried fixing that and got another import error (when trying to load qgis._core, despite _core.pyd being in what seemed to me to be an appropriate location). So I can't give you a full answer. – Dan Getz Jan 03 '20 at 14:26
  • The accepted answer to another question says to also add something to your system's PATH (note: not the same thing as system.path). Did you try this? Also, since you have OSGeo4W installed, are you running IDLE from there? From what the answer I linked said, I would expect that might fix the problem, without needing to change any paths or PATHs. – Dan Getz Jan 03 '20 at 14:38
  • Thank you. I will look. FYI, I am a novice with Python. Only a few weeks in and learning on the fly. QGIS the same. I am not sure how to run IDLE from within OSGeo4W. – Todd Jan 03 '20 at 14:50
  • 1
    Unfortunately I don't have OSGeo4W installed on my machine to try it myself. I found some information about that in this question and this other one. What is your end goal with using the QGIS libraries? I noticed that the code example you posted did not use any of them—you could delete everything above import subprocess. Are you trying to write code that works without opening QGIS? Or code to run inside a running QGIS? – Dan Getz Jan 03 '20 at 17:32
  • 1
    I’m trying to create a script to run outside of QGIS. Guess no particular reason to do so other than trying to grow my programming skills. – Todd Jan 04 '20 at 15:17

0 Answers0