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

vasudevram vasudevram at gmail.com
Sat Dec 23 10:35:07 EST 2006


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




More information about the Python-list mailing list