0

Problem

I have a gdal2tiles.bat file that says

@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
python "%OSGEO4W_ROOT%\apps\Python39\Scripts\gdal2tiles.py" %*

However, when I run the script from Powershell as gdal2tiles earth.tif or gdal2tiles.bat earth.tif, the error message says

The system cannot find the path specified. python: can't open file 'C:\apps\Python39\Scripts\gdal2tiles.py': [Errno 2] No such file or directory

Workaround

I edit the .bat file to put the file path of the .py file like the code below. I run it and it says The system cannot find the path specified. then the script runs as intended with correct output.

@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
python "C:\OSGeo4W\apps\Python39\Scripts\gdal2tiles.py" %*

However, the gdal2tiles.py is just one of the many Python scripts in the folder. If I need to run a different script, I need to edit its .bat file again.

Batch files in folder

My previous post is similar to this https://gis.stackexchange.com/q/414053/146811

What's a good fix that doesn't need me to edit all the .bat files?

BallpenMan
  • 185
  • 1
  • 12
  • Nevermind, please see this thread for a better workaround https://gis.stackexchange.com/a/414089/146811 – BallpenMan Oct 16 '21 at 07:11
  • If it works without using the variable `%OSGEO4W_ROOT%`, and that variable worked to launch `"%OSGEO4W_ROOT%\bin\o4w_env.bat"`, then clearly the issue is that something in `o4w_env.bat` is undefining that variable. In order to identify the issue, we would need to see the content of `o4w_env.bat`. Please do not crosspost your questions, if you have decided that the poor advice you're receiving over there is satisfactory, then please delete your question here. – Compo Oct 16 '21 at 13:21
  • Where/how do you set the variable `OSGEO4W_ROOT`? – aschipfl Oct 16 '21 at 17:42
  • @aschipfl Edit the system variables > Environment Variables... > make new System Variable – BallpenMan Oct 17 '21 at 07:09
  • And what is its value? type `set OSGEO4W_ROOT` into a Command Prompt window to verify… – aschipfl Oct 17 '21 at 10:09
  • It's `C:\OSGeo4W` – BallpenMan Oct 18 '21 at 11:44
  • Perhaps you need to put `cd /D "%~dp0."` as the second line of the batch script, so an argument like `earth.tif` is searched in the parent directory of the batch script (which is what I assume)… – aschipfl Oct 20 '21 at 14:30

0 Answers0