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

M

Michele Simionato

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
 
T

Thomas Heller

Michele said:
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
 
C

commander.coder

Michele said:
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
 
C

Carsten Haese

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
 
H

Hendrik van Rooyen

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
 
M

Michele Simionato

Hendrik said:
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.
"""
 
P

Peter Wang

Michele said:
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.
 
M

Michael B. Trausch

Peter said:
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
 
T

Tim Golden

[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.</dump>

TJG
 
H

Hendrik van Rooyen

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
 
V

vasudevram

Peter said:
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.
 
V

vasudevram

vasudevram said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,069
Latest member
SimplyleanKetoReviews

Latest Threads

Top