Re: Python program as a command-line program
Barry Warsaw writes: > On Jun 23, 2016, at 11:17 AM, Ben Finney wrote: > > >There isn't, AFAIK, anything portable that I can write in the shebang > >to turn a command invocation of ‘./foo/bar.py’ into ‘python3 -m > >foo.bar’. > > Why not just have a file that contains only? > > exec python3 -m foo.bar Because that's not something I can write in the shebang of an existing Python file, and expect it to work as I move around the directory that contains the file. There's no obvious way to tell the kernel (via the shebang line in the Python file) how to execute this file via a Python interpreter, without hitting the failure scenarios documented in PEP 366. This is unlike Perl. Heck, it's unlike Make and Awk and Sed. The case I'm making is that Python breaks the “put the interpreter command in the shebang, and it'll work” criterion for Python to be called a Unix scripting language. -- \ “I am amazed, O Wall, that you have not collapsed and fallen, | `\since you must bear the tedious stupidities of so many | _o__) scrawlers.” —anonymous graffiti, Pompeii, 79 CE | Ben Finney
Re: Python program as a command-line program (was: Python package providing both modules and an app)
On Jun 23, 2016, at 11:17 AM, Ben Finney wrote: >There isn't, AFAIK, anything portable that I can write in the shebang to >turn a command invocation of ‘./foo/bar.py’ into ‘python3 -m foo.bar’. Why not just have a file that contains only? exec python3 -m foo.bar Cheers, -Barry
Python program as a command-line program (was: Python package providing both modules and an app)
Barry Warsaw writes: > On Jun 22, 2016, at 11:25 AM, Ben Finney wrote: > > >There is a compounding tendency to disparage ‘python3 ./foo/bar.py’, > >[…]. I wish this trend could be effectively reversed, because IMO > >this is a serious impediment to considering Python a good choice for > >command-line scripting. > > Sorry, I don't quite understand the above. Do you mean that you would > rather use `python3 ./foo/bar.py` more often and `python3 -m foo.bar` > less often? When ‘./foo/bar.py’ has a shebang of ‘#! /usr/bin/python3’, and I invoke that path as a command, the Unix shebang semantics turn that into ‘/usr/bin/python3 ./foo/bar.py’. Python will run the file as the main module. But invoking it that way runs into the nastiness and constraints described in PEP 366 (primarily, relative imports break). There isn't, AFAIK, anything portable that I can write in the shebang to turn a command invocation of ‘./foo/bar.py’ into ‘python3 -m foo.bar’. So yes, I expect Unix shebang semantics to work, and the fact that design decisions in Python's import mechanism defeat that mode is an impediment to using Python for writing command-line programs. -- \ “Dvorak users of the world flgkd!” —Kirsten Chevalier, | `\rec.humor.oracle.d | _o__) | Ben Finney