[GIS] OSgeo4W64 Windows 7 & ImportError: No Module Named Site

batchgdal2tilesosgeo4wpython-2.7

I realize this question has been asked a lot, but I was not able to resolve my issue through the answers posted elsewhere… I am new to Python, gdal, OSgeo4W, etc. but have experience with ArcGIS.

I'm receiving the dreaded "ImportError: No module named site" error from Windows Cmd after running the following batch file by clicking on it directly from windows explorer (it is save as a .bat file).

ECHO Off
C:\OSGeo4W64\bin\gdalbuildvrt.exe depth_max.vrt *.tif
python C:\OSGeo4W64\bin\gdal2tiles.py -s EPSG:2278 -z 12 -w none "depth_max.vrt" depth_max
pause

Couple observations/comments:

  • The file runs fine when running it from the OSgeo4W window
  • gdalbuildvrt.exe executes fine in either scenario, the error is associated with the python command
  • Python 2.7 is installed at C:\Python27\ArcGIS10.2\ and C:\OSGeo4W64\apps\Python27
  • Python 3.6 is installed at C:\OSGeo4W64\apps\Python36
  • Running Windows 7 (Note: This may be run on Windows 10 as well)
  • gdalbuildvrt does not have a .py file; only .exe, which I found odd but I guess it is not a python script but a standalone program?
  • I tried including the SET commands for PYTHONHOME and PYTHONPATH in the batch file and it either did not work or broke python (it would then say DLLs were not found)

My eventual goal is to run this from a SHELL command via another program.

Best Answer

Two things (and @rumski20) helped to get this going:

  1. I was referencing Python27 not realizing that gdal2tiles requires Python36
  2. I referenced every .exe or .py directly

Probably a better way to do this, but it works for now.

SET GDAL_DATA=C:\OSGeo4W64\apps\gdal-dev\data
SET GDAL_DRIVER_PATH=C:\OSGeo4W64\bin\gdalplugins
SET OSGEO4W_ROOT=C:\OSGeo4W64
SET PATH=%OSGEO4W_ROOT%\bin;%PATH%
SET PYTHONHOME=%OSGEO4W_ROOT%\apps\Python36
SET PYTHONPATH=%OSGEO4W_ROOT%\apps\Python36

C:\OSGeo4W64\bin\gdalbuildvrt.exe depth_max.vrt Depth*.tif
C:\OSGeo4W64\apps\Python36\python.exe
C:\OSGeo4W64\apps\Python36\Scripts\gdal2tiles.py -s EPSG:2278 -z 12-17 -w none depth_max.vrt depth_max