[gdal-dev] gdal-utils: understanding the __main__ pattern

Matt Wilkie maphew at gmail.com
Fri Mar 4 14:19:05 PST 2022


Hello folks who know about the python script construction :)

The pattern used in all of the python scripts under swig/python/gdal-utils
is foreign to me. Can someone explain what's going on and why it's used?
(or point to where this has been explored already).

if __name__ == '__main__':
    sys.exit(main(sys.argv))

Gdal utils .py all end with the block above. Everything else I've used to
this point are more like:

if __name__ == '__main__':
    main(sys.argv)

The motivation for asking (besides just being curious about something new!)
is that I'm trying to figure out how we can use pip to create standard
launch wrappers in PYTHONHOME\Scripts instead of manual post-install setup
of the batch files we use now. When I change setup.py to do this via
console_scripts the resulting wrappers fail on this main construct. So I
need to understand what the original intent is to try and come up with a
remedy.

```
» gdal_edit fooo
Traceback (most recent call last):
  File "C:\Users\Matt\.conda\envs\gdal\Scripts\gdal_edit-script.py", line
33, in <module>
    sys.exit(load_entry_point('gdal-utils', 'console_scripts',
'gdal_edit')())
TypeError: main() missing 1 required positional argument: 'argv'
```

Thanks!

Matt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20220304/82831758/attachment.html>


More information about the gdal-dev mailing list