5

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' ?

Sreeraj
  • 729
  • 6
  • 23
  • 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
  • Use your osgeo4w shell and py3_env. See this answer - https://gis.stackexchange.com/a/277842/2856 – user2856 Oct 13 '19 at 22:31
  • 1
  • @user2856 I executed OSGeo4W.bat (in C:\OSGeo4W64) and typed 'py3_env' which sets my PYTHONHOME and correct PATH. Then called 'python3' and worked well. Still, I got the same error when I executed my program. Then, when I checked in the 'Environment Variables', I am still unable to find the variable 'PYTHONHOME' and none of the values of 'PATH' which was mentioned in OSGeo4W.bat (after running 'py3_env'). What should I do now ? – Sreeraj Oct 13 '19 at 23:25
  • @user2856 I updated this question (check the last part) after doing your suggested solution. Still, I am getting the same error. How can I solve this ? – Sreeraj Oct 13 '19 at 23:36
  • 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 01:36
  • 2
    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 03:59
  • have you tried python -m pip install encoding (or encoding-tool?) after setting OSGeo4W to python3 environnement? – LeoC Oct 14 '19 at 11:38
  • @LeoC When I tried python -m pip install encoding , I got the issue ERROR: Could not find a version that satisfies the requirement encoding (from versions: none) ERROR: No matching distribution found for encoding. – Sreeraj Oct 14 '19 at 11:46
  • @user2856 As you suggested, I opened OSGeo4W command prompt, then executed py3_env, qt5_env and ran the script using python3 path\to\your\script.py . This solved the issue. Yes, I got some QGIS import error, but I can solve that. Thanks. – Sreeraj Oct 17 '19 at 08:57

1 Answers1

5

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 .

Sreeraj
  • 729
  • 6
  • 23