How to run script from interpreter?

Rolander, Dan Dan.Rolander at marriott.com
Fri Jan 19 08:44:27 EST 2001


See, I knew it was something simple. Thanks for pointing me in the right
direction!

One more thing, is there a way to pass a command line parameter to the
script named in execfile? Say if I have a script to test which expects a
value in sys.argv[1]?

Thanks,
Dan

-----Original Message-----
From: Mikael Olofsson [mailto:mikael at isy.liu.se]
Sent: Friday, January 19, 2001 6:52 AM
To: Dan Rolander
Cc: python-list at python.org
Subject: Re: How to run script from interpreter?



On 19-Jan-01 Dan Rolander wrote:
 >  This doesn't work for me. I know how to import and how to use the if
 >  __name__ trick, but how can I execute a script from the interactive
Python
 >  interpreter? That is, can I load a script and have it run from inside
the
 >  interpreter? (I'm sure this is easy I'm just not seeing it.)

Let's see... I think it was here somewhere... Ah, yes: In Python Library 
Reference, 2.3 Built-in Functions, at

  http://www.python.org/doc/current/lib/built-in-funcs.html

execfile (file[, globals[, locals]]) 
     This function is similar to the exec statement, but parses a file 
     instead of a string. It is different from the import statement in 
     that it does not use the module administration -- it reads the file 
     unconditionally and does not create a new module.2.8 

     The arguments are a file name and two optional dictionaries. The 
     file is parsed and evaluated as a sequence of Python statements 
     (similarly to a module) using the globals and locals dictionaries
     as global and local namespace. If the locals dictionary is omitted 
     it defaults to the globals dictionary. If both dictionaries are 
     omitted, the expression is executed in the environment where 
     execfile() is called. The return value is None. 

HTH

/Mikael

-----------------------------------------------------------------------
E-Mail:  Mikael Olofsson <mikael at isy.liu.se>
WWW:     http://www.dtr.isy.liu.se/dtr/staff/mikael               
Phone:   +46 - (0)13 - 28 1343
Telefax: +46 - (0)13 - 28 1339
Date:    19-Jan-01
Time:    12:35:02

         /"\
         \ /     ASCII Ribbon Campaign
          X      Against HTML Mail
         / \

This message was sent by XF-Mail.
-----------------------------------------------------------------------




More information about the Python-list mailing list