Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
On 2018-08-16 14:33, Chris Angelico wrote: > On Thu, Aug 16, 2018 at 8:32 PM, Thomas Jollans wrote: >> On 2018-08-16 01:05, Chris Angelico wrote: >>> On Thu, Aug 16, 2018 at 8:51 AM, Cameron Simpson wrote: And as an additional alternative, when I want something weird (extra python args or the like) I usually make my script.py into a module and invoke it via a shell script, eg: #!/bin/sh exec /particular/python python-opts... -m script_module ${1+"$@"} Obviously that'd need a little adaption under Windows. >>> >>> Since an executable file without a shebang should normally be invoked >>> through /bin/sh, you can actually combine this technique into the >>> script itself with a cool hack: >> >> Well, sorta. Executable text files without a shebang line are not >> executable per se, but most shells pretend they are. If you try to run a >> shebang-less script through, say, Python's subprocess module, it won't work. > > Good point. Still, for a lot of situations, it does allow you to > invoke the .py file. I wonder if there's some sort of sneaky way to > make the exec line appear as a comment to Python - probably involving > quoting rules. Easy as pie: (with an extra line) '''exec' /usr/bin/env python3 -u -E -W error "$0" "$@" ''' import sys print('hello', sys.executable) -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
On Thu, Aug 16, 2018 at 8:32 PM, Thomas Jollans wrote: > On 2018-08-16 01:05, Chris Angelico wrote: >> On Thu, Aug 16, 2018 at 8:51 AM, Cameron Simpson wrote: >>> And as an additional alternative, when I want something weird (extra python >>> args or the like) I usually make my script.py into a module and invoke it >>> via a shell script, eg: >>> >>> #!/bin/sh >>> exec /particular/python python-opts... -m script_module ${1+"$@"} >>> >>> Obviously that'd need a little adaption under Windows. >> >> Since an executable file without a shebang should normally be invoked >> through /bin/sh, you can actually combine this technique into the >> script itself with a cool hack: > > Well, sorta. Executable text files without a shebang line are not > executable per se, but most shells pretend they are. If you try to run a > shebang-less script through, say, Python's subprocess module, it won't work. Good point. Still, for a lot of situations, it does allow you to invoke the .py file. I wonder if there's some sort of sneaky way to make the exec line appear as a comment to Python - probably involving quoting rules. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
On 2018-08-16 01:05, Chris Angelico wrote: > On Thu, Aug 16, 2018 at 8:51 AM, Cameron Simpson wrote: >> And as an additional alternative, when I want something weird (extra python >> args or the like) I usually make my script.py into a module and invoke it >> via a shell script, eg: >> >> #!/bin/sh >> exec /particular/python python-opts... -m script_module ${1+"$@"} >> >> Obviously that'd need a little adaption under Windows. > > Since an executable file without a shebang should normally be invoked > through /bin/sh, you can actually combine this technique into the > script itself with a cool hack: Well, sorta. Executable text files without a shebang line are not executable per se, but most shells pretend they are. If you try to run a shebang-less script through, say, Python's subprocess module, it won't work. > > > exec /usr/local/bin/python3 -x -W error $0 "$@" > """ > This is an example script. > > It is executable and will invoke itself through Python. > """ > import warnings > warnings.warn("This should be an error.") > print("This shouldn't happen.") > > > The "-x" parameter means "skip the first line", and in a sense, the > exec line is a non-standard shebang. :) -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
David Raymond writes: > So what are you saying is an option vs an argument? Because I see no > distinction whatsoever. The command-line conventions do recognise the distinction. * A command-line argument specifies input to the program. For example, the destination file for a ‘cp’ command is specified as the final argument to that command. * A command-line option specifies a modifier to the program's behaviour. For example, the ‘--force’ option for a ‘cp’ command modifies the behaviour to remove the destination file if it already exists. > When you run something you give it a bunch of strings. Sure, command-line arguments and options are specified as strings. That doesn't erase the distinction; it just means there is a category they both belong to. > That's it. You can claim to not see the distinction, but that's athwart existing convention, and you'll need to accept that there *is* such a distinction in the specification of a great many programs. > There is nothing magical about putting a dash in front of a letter, Convention is not magical, true. Specifying options with a leading hyphen is arbitrary. That doesn't make it meaningless; the convention exists and is very normal. https://en.wikipedia.org/wiki/Command-line_interface#Arguments> -- \ “In the long run, the utility of all non-Free software | `\ approaches zero. All non-Free software is a dead end.” —Mark | _o__)Pilgrim, 2006 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
Thanks to everyone who contributed to this thread. Great feedback and suggestions! - Malcolm -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
On Thu, Aug 16, 2018 at 8:51 AM, Cameron Simpson wrote: > And as an additional alternative, when I want something weird (extra python > args or the like) I usually make my script.py into a module and invoke it > via a shell script, eg: > > #!/bin/sh > exec /particular/python python-opts... -m script_module ${1+"$@"} > > Obviously that'd need a little adaption under Windows. Since an executable file without a shebang should normally be invoked through /bin/sh, you can actually combine this technique into the script itself with a cool hack: exec /usr/local/bin/python3 -x -W error $0 "$@" """ This is an example script. It is executable and will invoke itself through Python. """ import warnings warnings.warn("This should be an error.") print("This shouldn't happen.") The "-x" parameter means "skip the first line", and in a sense, the exec line is a non-standard shebang. :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
On 15Aug2018 20:54, eryk sun wrote: On Wed, Aug 15, 2018 at 9:22 AM, Thomas Jollans wrote: If you really want to, you can pass a *single* argument in your #! line, e.g.: #!/usr/bin/python3 -Wd This works for options that can be grouped into a single argument. Multiple -X options aren't supported, nor is combining a -X option with other options. Using a shebang also works in Windows if .py files are associated with the py.exe launcher, which can handle multiple arguments in the shebang instead of just one. And as an additional alternative, when I want something weird (extra python args or the like) I usually make my script.py into a module and invoke it via a shell script, eg: #!/bin/sh exec /particular/python python-opts... -m script_module ${1+"$@"} Obviously that'd need a little adaption under Windows. This has a few advantages: - you can use several python-opts - your script, unless very trivial, will usually have some reusable pieces; by making it a module with a main() function you can make those pieces available for _other_ modules to import and use! Making your script a module may seem like some extra effort to little benefit, but after you've done it a few times it is easy and I at least now have a suite of little modules whose components get reused all the time. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
On Wed, Aug 15, 2018 at 9:22 AM, Thomas Jollans wrote: > > If you really want to, you can pass a *single* argument in your #! line, > e.g.: > > #!/usr/bin/python3 -Wd This works for options that can be grouped into a single argument. Multiple -X options aren't supported, nor is combining a -X option with other options. Using a shebang also works in Windows if .py files are associated with the py.exe launcher, which can handle multiple arguments in the shebang instead of just one. -- https://mail.python.org/mailman/listinfo/python-list
RE: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
So what are you saying is an option vs an argument? Because I see no distinction whatsoever. When you run something you give it a bunch of strings. That's it. There is nothing magical about putting a dash in front of a letter, nothing magical about putting in a string that might possibly also be a file name. The only things that might matter are white space and quote characters, because really all you're doing is giving the shell or OS a string, and it decides what to run and what is the resulting (ordered) list of strings which it will then pass to that program. Being able to just run "script.py" is just a convenience provided by the OS. It goes "ohh, after I've parsed it, that first token matches up with an existing file, and my records say that that extension can be opened with this python.exe program, so I'm just gonna run that python.exe thing and pass it the ordered list of everything I just got." In this specific case, the people who wrote python.exe decided that when it goes through the list of strings which it was given, that that first thing that doesn't start with a dash is the "file name", that anything before that "file name" are things it will look at right now, and anything after the "file name" are things it will throw into sys.argv and let later execution decide what to do with. Since the convenience method uses the first bit as the "file name" to determine which convenience program to run, and the python.exe program just tosses anything after the "file name" into sys.argv, then to get anything as an "argument" to python.exe and not the script, then you either need to run it as "python.exe bunch of strings to pass to python.exe", or the more difficult method of "muck about with the OS's convenience method to get it to do something magical." -Original Message- From: Python-list [mailto:python-list-bounces+david.raymond=tomtom@python.org] On Behalf Of Malcolm Greene Sent: Tuesday, August 14, 2018 7:47 PM To: python-list@python.org Subject: Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter? > You might try: > from getopt import getopt > or the (apparently newer): > from optparse import OptionParser Thanks Mike. My question was trying to make a distinction between Python options (flags that precede the script or module name) and arguments (the script specific values passed on the command line following the script's name). Here's a description of the options I'm referring to: https://docs.python.org/3/using/cmdline.html#generic-options -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
On 14/08/18 23:45, Malcolm Greene wrote: > When you run a script via "python3 script.py" you can include command > line options like -b, -B, -O, -OO, etc between the "python3" interpreter > reference and the script.py file, eg. "python3 -b -B -O -OO script.py". > When you create a script that is executable directly, eg. script.py with > execution bit set on Linux or on Windows where the .py file extension is > associated with a specific Python executable, there doesn't appear to be > a way to pass command line options to the script. In this later case, > how can I pass my script command line options without having these > options confused with command line arguments? > Thank you, > Malcolm > If you really want to, you can pass a *single* argument in your #! line, e.g.: #!/usr/bin/python3 -Wd -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
> You might try: > from getopt import getopt > or the (apparently newer): > from optparse import OptionParser Thanks Mike. My question was trying to make a distinction between Python options (flags that precede the script or module name) and arguments (the script specific values passed on the command line following the script's name). Here's a description of the options I'm referring to: https://docs.python.org/3/using/cmdline.html#generic-options -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
> If you run the script directly, by entering >script.py or clicking a script > icon or name in File Explorer, it runs python without python options *other > than those specified in environmental variables*. Understood. I thought there might have been a way to pass Python option values via a single environment variable (something like PYTHONOPTIONS) vs individual environment variables. Thank you Malcolm -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
On 2018-08-14 16:45, Malcolm Greene wrote: > When you run a script via "python3 script.py" you can include command > line options like -b, -B, -O, -OO, etc between the "python3" interpreter > reference and the script.py file, eg. "python3 -b -B -O -OO script.py". > When you create a script that is executable directly, eg. script.py with > execution bit set on Linux or on Windows where the .py file extension is > associated with a specific Python executable, there doesn't appear to be > a way to pass command line options to the script. In this later case, > how can I pass my script command line options without having these > options confused with command line arguments? You might try: from getopt import getopt or the (apparently newer): from optparse import OptionParser These appear to both be deprecated in favor of argparse. I haven't made the switch myself because argparse appears (upon a cursory reading of the documentation) to muddle the difference between options and arguments. -- Michael F. Stemper Indians scattered on dawn's highway bleeding; Ghosts crowd the young child's fragile eggshell mind. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
On 8/14/2018 5:45 PM, Malcolm Greene wrote: When you run a script via "python3 script.py" you can include command line options like -b, -B, -O, -OO, etc between the "python3" interpreter reference and the script.py file, eg. "python3 -b -B -O -OO script.py". More generally, python script.py
How to pass Python command line options (vs arguments) when running script directly vs via Python interpreter?
When you run a script via "python3 script.py" you can include command line options like -b, -B, -O, -OO, etc between the "python3" interpreter reference and the script.py file, eg. "python3 -b -B -O -OO script.py". When you create a script that is executable directly, eg. script.py with execution bit set on Linux or on Windows where the .py file extension is associated with a specific Python executable, there doesn't appear to be a way to pass command line options to the script. In this later case, how can I pass my script command line options without having these options confused with command line arguments? Thank you, Malcolm -- https://mail.python.org/mailman/listinfo/python-list