473,403 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

script that parses command line, and execfile('')

TP
Hello,

I have a script that uses the "optparse" package to parse the command line.
For example:

$ script.py --help
# displays help about script.py

Is this possible to call such a script with execfile('') once in the Python
interactive shell?
>>execfile( 'script.py' )
I get errors because there is no argv dictionary when used with execfile.

How to solve this problem, so that I am able to use script.py in command
line as well as with execfile?

Thanks

Julien

--
python -c "print ''.join([chr(154 - ord(c)) for c in '*9(9&(18%.9&1+,\'Z
(55l4('])"

"When a distinguished but elderly scientist states that something is
possible, he is almost certainly right. When he states that something is
impossible, he is very probably wrong." (first law of AC Clarke)
Nov 3 '08 #1
4 7053
TP <Tr**********@Paralleles.invalidwrites:
Hello,

I have a script that uses the "optparse" package to parse the command line.
For example:

$ script.py --help
# displays help about script.py

Is this possible to call such a script with execfile('') once in the Python
interactive shell?
>>>execfile( 'script.py' )

I get errors because there is no argv dictionary when used with execfile.

How to solve this problem, so that I am able to use script.py in command
line as well as with execfile?
Have you tried setting sys.argv manually?

e.g.
>>import sys
sys.argv = ['--help']
execfile('script.py')
But I have to say I have never felt the need to use execfile() this way.

--
Arnaud
Nov 3 '08 #2
TP schrieb:
Hello,

I have a script that uses the "optparse" package to parse the command line.
For example:

$ script.py --help
# displays help about script.py

Is this possible to call such a script with execfile('') once in the Python
interactive shell?
>>>execfile( 'script.py' )

I get errors because there is no argv dictionary when used with execfile.

How to solve this problem, so that I am able to use script.py in command
line as well as with execfile?

Don't use execfile. Make script.py like this:
....

def main(argv=None):
if argv is None: argv = sys.argv[1:]
...
Then just do

import script
script.main(arguments)
instead.

Diez
Nov 3 '08 #3
On Nov 4, 12:43*am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
def main(argv=None):
* * *if argv is None: argv = sys.argv[1:]
* * *...
Wouldn't that make optparse miss the first parameter sys.argv[1]
mistaking it to be the name of the current program?

- Sandip
Nov 4 '08 #4
Sandip Bhattacharya wrote:
On Nov 4, 12:43 am, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>def main(argv=None):
if argv is None: argv = sys.argv[1:]
...

Wouldn't that make optparse miss the first parameter sys.argv[1]
mistaking it to be the name of the current program?
Nope...optparse uses argv[1:] as documented at [1]. The "prog"
argument can be specified in the constructor to OptionParser, but
defaults to sys.argv[0] if it's not been explicitly specified.[2]

-tkc
[1]
http://www.python.org/doc/2.5.2/lib/...arguments.html

[2]
http://www.python.org/doc/2.5.2/lib/...ng-parser.html
(at the "prog" entry at the bottom)
Nov 4 '08 #5

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

Similar topics

8
by: Sticks | last post by:
ok... im not quite sure how to describe my problem. i have a php script that runs through my entire php site and writes the resulting output to html files. this is necessary as the nature of the...
2
by: Jonathan | last post by:
I'm puzzled by Python's behavior when binding local variables which are introduced within exec() or execfile() statements. First, consider this simple Python program: # main.py def f() : x = 1...
4
by: Ian Giblin | last post by:
I am an experienced C programmer, learning C++ by writinging a mathematical toolkit in the framework of a script interpreter. I am posting here to ask for advice (or references) on the object...
3
by: olaufr | last post by:
Hi, I need to call a python script, with command line arguments (it is an autonomous script with a __main__), from within another python script. Can I use exec() or execfile() for this? How to...
1
by: Brian Blais | last post by:
Hello, I'd like to do the following: import sys try: execfile("somefile.py") except: s=sys.exc_info() print "Error '%s' happened on line %d" % (s,s.tb_lineno)
5
by: Helmut Jarausch | last post by:
Hi, I'm still looking for an elegant and clear means to terminate the main script in Python. Unfortunately, Python doesn't allow a 'return' instruction in the main script. Using...
5
by: Bjorn Sagbakken | last post by:
Hello I have just migrated from VS 2003 to VS 2005, and .NET framework 1.1 to 2.0 I am at the end of debugging and fixing stuff. Now there is one error I just cannot find a solution to: On...
6
by: Alan Isaac | last post by:
I'm fairly new to Python and I've lately been running a script at the interpreter while working on it. Sometimes I only want to run the first quarter or half etc. What is the "good" way to do...
4
by: Chris8Boyd | last post by:
I am embedding Python in a MSVC++ (2005) application. The application creates some environment and then launches a Python script that will call some functions exported from the MSVC++ application....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.