qgis – Python ImportError: No Module Named Builtins in QGIS

importerrorpyqgis-3pythonqgis

When I am executing a python script for doing a geoprocessing task using QGIS Desktop 3.8.3 (executed using a windows .bat file), I am getting an import error.

from builtins import zip
ImportError: No module named builtins 

In C:\OSGeo4W64\apps\, I can find folders namely Python27 and Python37. I have installed Python 3.7 version before installing OSGeo4W64.

The .bat file (from which I am executing this .py file) is :

@echo off
call C:\OSGeo4W64\bin\o4w_env.bat
call C:\OSGeo4W64\bin\qt5_env.bat
call C:\OSGeo4W64\bin\py3_env.bat
REM Change OSGEO4W_ROOT to point to the base install folder
set QGIS_PREFIX_PATH=C:\OSGeo4W64\apps\qgis
REM Gdal Setup
set GDAL_DATA=C:\OSGeo4W64\share\gdal\
REM Python Setup
set PATH=C:\OSGeo4W64\bin;C:\OSGeo4W64\apps\qgis\bin;%PATH%
SET PYTHONHOME=C:\OSGeo4W64\apps\Python27
set PYTHONPATH=C:\OSGeo4W64\apps\qgis\python;%PYTHONPATH%

REM Launch python job
python C:/V/V.py
pause

The .py file starts like :

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 * 

In Environment Variables, in User variables,

Path : C:\Users\sreeraj\AppData\Local\Programs\Python\Python37\Scripts\, C:\Users\sreeraj\AppData\Local\Programs\Python\Python37\
OSGEO4W_ROOT : C:\OSGeo4W64 

And in System variables,

Path : C:\Users\sreeraj\AppData\Local\Programs\Python\Python37\Scripts\, C:\Users\sreeraj\AppData\Local\Programs\Python\Python37\ 

Inside python-core.bat in C:\OSGeo4W64\etc\ini\, I can find

SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
PATH %OSGEO4W_ROOT%\apps\Python37\Scripts;%PATH% 

But I can't find any variables like PYTHONPATH or PYTHONHOME in Environment Variables (I don't know whether this information will be useful for solving this importerror or not.)

How can I solve this ImportError : No module named builtins ?

Best Answer

builtins module does not exists in Python 2 (it's called __builtin__). So probably you are using the wrong Python interpreter.

Just to be sure try to print sys.version_info to know which version are you using, and sys.executable to get the absolute path to the interpreter.

I have no experience on Windows but probably you can use the Python 3.7 interpreter changing:

SET PYTHONHOME=C:\OSGeo4W64\apps\Python27

with

SET PYTHONHOME=C:\OSGeo4W64\apps\Python37
Related Question