2

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.

5
  • Sounds like a python path issue. Could you try calling python like C:\OSGeo4W64\bin\python.exe in your script (or, conversely, navigate to that folder cd C:\OSGeo4W64\bin)?
    – rumski20
    Commented Jul 12, 2017 at 18:41
  • C:\OSGeo4W64\apps\Python27\python.exe did not work, same error; however, using C:\OSGeo4W64\apps\Python27\pythonw.exe did not give the error but also did not run gdal2tiles. Commented Jul 12, 2017 at 19:18
  • You may try alternative installer - NextGIS QGIS from here nextgis.com/nextgis-qgis (own python included, so no problem with system paths). Commented Jul 13, 2017 at 9:11
  • I would prefer not to, if possible. OSGeo4W should be sufficient. Commented Jul 13, 2017 at 13:41
  • Did you try the path to Python from within \bin?
    – rumski20
    Commented Jul 13, 2017 at 19:13

2 Answers 2

3

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
1

As @drivetosave answer is quite old, i had some struggle to find the right combination when facing the same problem. Here is what i've done :

when going to control panel > system > advanced system parameters > environnement variable, i added to variable for user

OSGEO4W_ROOT as C:\OSGeo4W64

PYTHONHOME as C:\OSGeo4W64\apps\Python37

PYTHONPATH as C:\OSGeo4W64\apps\Python37

1
  • don't forget to delete any path variables that you added. Commented Mar 10, 2020 at 1:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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