469,654 Members | 1,857 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,654 developers. It's quick & easy.

How a script can know if it has been called with the -i command line option?

The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?
TIA,

Michele Simionato

Dec 21 '06 #1
11 1245
Michele Simionato schrieb:
The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?
TIA,

Michele Simionato
I don't know how to get the command line flags, but the variable you are interested
in is this one:

from ctypes import *
print c_int.in_dll(pythonapi, "Py_InteractiveFlag")

;-)

Thomas

Dec 21 '06 #2

Michele Simionato wrote:
The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?
In the optparse module.

Jim

Dec 21 '06 #3
On Thu, 2006-12-21 at 11:22 -0800, co*************@hotmail.com wrote:
Michele Simionato wrote:
The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?
In the optparse module.
That doesn't answer the question. The OP wants to inspect the options
passed to the interpreter, not the options passed to the script.
optparse aids in parsing sys.argv, which only contains the options that
are passed to the script.

-Carsten
Dec 21 '06 #4
"Michele Simionato" <mi***************@gmail.comwrote:

The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?
sys.argv() ?

- Hendrik

Dec 22 '06 #5
Hendrik van Rooyen wrote:
"Michele Simionato" <mi***************@gmail.comwrote:

The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?

sys.argv() ?

- Hendrik
No, read what Carsten said:
"""
That doesn't answer the question. The OP wants to inspect the options
passed to the interpreter, not the options passed to the script.
optparse aids in parsing sys.argv, which only contains the options that
are passed to the script.
"""

Dec 22 '06 #6

Michele Simionato wrote:
The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?
I realize this is quite a hack, but the entire command line is
preserved in the process's entry in the OS's process table. if you do
"ps -ax" you will see that the interpreter was invoked with -i. I
didn't test this under windows, but it works on Mac and Linux.

Dec 22 '06 #7
Peter Wang wrote:
Michele Simionato wrote:
>The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?

I realize this is quite a hack, but the entire command line is
preserved in the process's entry in the OS's process table. if you do
"ps -ax" you will see that the interpreter was invoked with -i. I
didn't test this under windows, but it works on Mac and Linux.
There is a set of utilities that have UNIX-like ps behavior, but, as is
typical for Windows, they don't work the way their UNIX and UNIX-like
counterparts do. Does 'ps' work from within Cygwin, and if so, would
redistributing that be an option?

-- Mike
Dec 22 '06 #8
Michele Simionato wrote:
The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py.
[Michael B. Trausch]
There is a set of utilities that have UNIX-like ps behavior, but, as is
typical for Windows, they don't work the way their UNIX and UNIX-like
counterparts do. Does 'ps' work from within Cygwin, and if so, would
redistributing that be an option?
If you wanted to get the command line from within
Windows, you could use win32api.GetCommandLine.
I think the OP's on Linux, and in any case you'd have
to do your own parsing, but...

<dump>
c:\>python -i
Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>import win32api
win32api.GetCommandLine ()
'python -i'
>>>
</dump>

TJG

Dec 22 '06 #9
"Michele Simionato" <mi***************@gmail.comwrote:

Hendrik van Rooyen wrote:
"Michele Simionato" <mi***************@gmail.comwrote:
but I don't see a way to retrieve the command line flags, where should
I look?
sys.argv() ?

- Hendrik

No, read what Carsten said:
"""
That doesn't answer the question. The OP wants to inspect the options
passed to the interpreter, not the options passed to the script.
optparse aids in parsing sys.argv, which only contains the options that
are passed to the script.
"""
True. - I was under the impression that sys.argv has all the stuff in it,
but it just ain't so - I made a file called junk.py containing two lines:

import sys
print sys.argv

and sys.argv just has junk.py in it, for both styles of command line.

I also noticed that if you do the following:

python junk.py -i

then the interpreter is not interactive. I was also not aware that
the options had positional dependency.

you could do a work around by calling it like this:

python -i junk.py -i

then sys.argv has the extra -i...

or better, insist on being told in both cases with say -i and -n at the end.

it does not answer the real question, though, as someone could lie to you.

- Hendrik
Dec 23 '06 #10

Peter Wang wrote:
Michele Simionato wrote:
The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?

I realize this is quite a hack, but the entire command line is
preserved in the process's entry in the OS's process table. if you do
"ps -ax" you will see that the interpreter was invoked with -i. I
didn't test this under windows, but it works on Mac and Linux.
That hack might not work - at least, as described, and on Linux or Mac
OS if the UNIX-based one, i.e. OS X). Because there could be other
users who ran python command lines with or without the -i option. As
described, there's no way for this user to know which python invocation
is his/hers, and which are of other users. There might be a way,
though, if we can get this user's python instance's process id and then
grep for a line containing that id (in the appropriate column) in the
ps output.

Vasudev Ram
~~~~~~~~~~~~~~~~~~~~~~~~~~
Dancing Bison Enterprises
http://www.dancingbison.com
http://dancingbison.blogspot.com
~~~~~~~~~~~~~~~~~~~~~~~~~~
Check out the cool Snap.com preview feature on my web site.
Free signup for anyone at www.snap.com
I'm not affiliated with it.

Dec 23 '06 #11

vasudevram wrote:
Peter Wang wrote:
Michele Simionato wrote:
The subject says it all, I would like a script to act differently when
called as
$ python script.py and when called as $ python -i script.py. I looked
at the sys module
but I don't see a way to retrieve the command line flags, where should
I look?
I realize this is quite a hack, but the entire command line is
preserved in the process's entry in the OS's process table. if you do
"ps -ax" you will see that the interpreter was invoked with -i. I
didn't test this under windows, but it works on Mac and Linux.

That hack might not work - at least, as described, and on Linux or Mac
OS if the UNIX-based one, i.e. OS X). Because there could be other
users who ran python command lines with or without the -i option. As
described, there's no way for this user to know which python invocation
is his/hers, and which are of other users. There might be a way,
though, if we can get this user's python instance's process id and then
grep for a line containing that id (in the appropriate column) in the
ps output.

Vasudev Ram
~~~~~~~~~~~~~~~~~~~~~~~~~~
Dancing Bison Enterprises
http://www.dancingbison.com
http://dancingbison.blogspot.com
~~~~~~~~~~~~~~~~~~~~~~~~~~
Check out the cool Snap.com preview feature on my web site.
Free signup for anyone at www.snap.com
I'm not affiliated with it.
Just realized: getting the python process's process id is possible from
the Python program itself, using os.getpid().

Vasudev

Dec 23 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Phil Powell | last post: by
reply views Thread by Phil Powell | last post: by
5 posts views Thread by deko | last post: by
13 posts views Thread by wattersmt | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.