473,795 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1440
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(py thonapi, "Py_Interactive Flag")

;-)

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.comwr ote:

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.comwr ote:

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.GetCom mandLine.
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.GetC ommandLine ()
'python -i'
>>>
</dump>

TJG

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

Hendrik van Rooyen wrote:
"Michele Simionato" <mi************ ***@gmail.comwr ote:
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
8297
by: Phil Powell | last post by:
Where can I find an online PHP form validator script library to use? I have tried hacking the one here at work for weeks now and it's getting more and more impossible to customize, especially now with form elements that turn out to be arrays that have to be compared with one another! I have one form element, languages, a checkbox group. Beside each checkbox is a dropdown, proficiency (which will become proficiency alongside languages)....
0
4670
by: Phil Powell | last post by:
<?php class FileRemoval { var $fileNameArray, $isRemoved, $errorMsg = ''; function FileRemoval() { // CONSTRUCTOR $this->fileNameArray = array(); $this->isRemoved = 0; }
5
9206
by: deko | last post by:
In regard to running php scripts with cron - Here is a sample script: <?php //debug.php echo "<br> This is a test"; ?> I can call debug.php from a web page on my site like this:
2
22714
by: jet | last post by:
Hi, Maybe this is an easy task, but I'm having a really hard time figuring out how to do this. I'm a complete newbie to SQL Server. I have a database dump file from MySQL that's in .sql format. I'm trying to figure out how to import that into SQL Server 2000 so that I'll be able to manipulate it in a gui format, rather than command line. I can't find any import that takes a .sql file. I've been trying to load it into the query...
9
13760
by: _link98 | last post by:
Normally I use the semicolon for statement delimiters in plain SQL scripts (for DDL, simple DML etc.). But inside SQL/PL I tend to use % or @ as statement delimiters. But other people prefer other delimiters. I know that I can use the CLP option "-td" to specify the delimiter on the command line, when invoking the script. But this requires me to look inside the scripts to find the delimiter before I run the script.
13
6012
by: wattersmt | last post by:
Hello, I am trying to write a python cgi that calls a script over ssh, the problem is the script takes a very long time to execute so Apache makes the CGI time out and I never see any output. The script is set to print a progress report to stdout every 3 seconds but I never see any output until the child process is killed. Here's what I have in my python script:
2
1578
by: rowan | last post by:
I'm writing a Python script that can either be called as a Cron job or as a web page (i.e. as a CGI in response to an HTTP request). This is to process the mailboxes on my web server (to which I don't have command line access) to remove old messages. How do I find out whether the script has been called as a Cron job or as a CGI? I need to know this so I can format the output correctly, e.g. if this is a web request I need to start the...
51
4164
by: Ojas | last post by:
Hi!, I just out of curiosity want to know how top detect the client side application under which the script is getting run. I mean to ask the how to know whether the script is running under Command Prompt or Browser or some other application? Ojas.
1
47492
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click on a link and after a moment or two a file download dialog box pops-up in your web browser and prompts you for some instructions, such as “open” or “save“. I’m going to show you how to do that using a perl script. What You Need Any recent...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3722
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.