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)
import processing
from processing.core.Processing import Processing
from processing.tools import *
import os, glob 

In the 'Environment Variables', in the 'path' of 'user variables' section, I can find :


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
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

"%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>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

Still, I am unable to solve this issue. How can I solve this QGIS import error : No module named 'qgis' ?

  • Can you check the value of PYTHONPATH and PYTHONHOME system variables? – alphabetasoup Oct 13 '19 at 19:23
  • @RichardLaw I am unable to find variables namely PYTHONPATH and PYTHONHOME in the System variables. If I create these 2 variable names in the System variables, then what variable values should I add to each of these variables ? – Sreeraj Oct 13 '19 at 21:47
  • 1
    Possible duplicate of OSGeo4W shell with python3 – user2856 Oct 13 '19 at 22:32
  • 1
    Are you actually typing SET PYTHONPATH=? If so, why? Just open the OSGeo4W command prompt, run py3_env, then python3 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 run qt5_env before running your script. – user2856 Oct 14 '19 at 1:36
  • 1
    Callingpy3_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. – user2856 Oct 14 '19 at 3:59

The solution to this issue is to open OSGeo4W command prompt, then execute py3_env, qt5_env and ran the script using python3 path\to\your\script.py .

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.