I have written a python code which uses the 'Raster Surface Volume' algorithm of QGIS Desktop 3.8.3. When I execute the python code, I am getting an error :
The system cannot find the path specified.
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
Current thread 0x00002218 (most recent call first):
Press any key to continue . . .
I have imported the libraries as given below.
from osgeo import ogr, gdal
from gdalconst import *
from qgis.core import *
import qgis.utils, sys
from qgis.gui import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QFileInfo
app = QApplication([])
QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)
QgsApplication.initQgis()
sys.path.append('C:/OSGeo4W64/apps/qgis/python/plugins')
import processing
from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *
import os, glob
In the 'Environment Variables', in the 'path' of 'user variables' section, I can find :
C:\Users\sreeraj\AppData\Local\Programs\Python\Python37\Scripts\
C:\Users\sreeraj\AppData\Local\Programs\Python\Python37\
I can also find the same in the 'path' of 'system variables' section in the 'Environment Variables'.
In the folder 'C:\OSGeo4W64\apps\', I can find 2 folders namely 'Python37' and 'Python27'.
I have installed 'python-3.7.4-amd64.exe' in my computer before installing the 'osgeo4w64'. Which means that I am using Python 3.7.4 in my computer and when I installed 'OSGeo4W64', I can find find a folder 'Python37' (along with 'Python27') which is integrated to QGIS Desktop 3.8.3.
Can anyone help me with the exact solution to this error 'No module named encodings' ?
As @user2856 suggested, I opened OSGeo4W command prompt, then executed py3_env, qt5_env and ran the script using python3 path\to\your\script.py . Now, I got a QGIS import error :
from qgis.core import * ModuleNotFoundError:
No module named 'qgis' .
python-qgis.bat file in C:\OSGeo4W64\bin\ is given below :
@echo off
call "%~dp0\o4w_env.bat"
call qt5_env.bat
call py3_env.bat
@echo off
path %OSGEO4W_ROOT%\apps\qgis\bin;%PATH%
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins
set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%PYTHONPATH%
"%PYTHONHOME%\python" %*
I have tried the accepted answer of @Germán Carrillo of Problem with import qgis.core when writing a stand-alone PyQGIS script by running var_entorno_osgeo4w.bat in OSGeo4W64 shell as given below :
run o-help for a list of available commands
C:\OSGeo4W64>var_entorno_osgeo4w.bat
C:\OSGeo4W64>set PYTHONPATH=C:\OSGeo4W64\apps\qgis\python
C:\OSGeo4W64>set PATH=C:\OSGeo4W64\apps\qgis\bin;C:\OSGEO4~1\apps\Python37\Scripts;C:\OSGEO4~1\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBem
C:\OSGeo4W64>
Still, I am unable to solve this issue. How can I solve this QGIS import error : No module named 'qgis' ?
PYTHONPATH
andPYTHONHOME
system variables?SET PYTHONPATH=
? If so, why? Just open the OSGeo4W command prompt, runpy3_env
, thenpython3 path\to\your\script.py
to run your script. You may hit some QGIS import errors, edit your post if you get that. You may alsdo need to runqt5_env
before running your script.py3_env
doesn't set anything permanently, it just set the correct environment variables for the OSGeo4W window, so you won't see any changes in your user environment variables.