Thanks,
Dan
import test
(you don't need the .py)
As for doing stuff in the script it will execute any commands it encounters.
It's neat to have all your stuff in functions and then have commands at the
bottom to call the functions as needed. This is like a simulated main() from
C.
if __name__ == '__main__': # only run if main program
DoStuff( )
Rolander, Dan <Dan.Ro...@marriott.com> wrote in message
news:mailman.979855413...@python.org...
This way I can similate everything because I can pass main whatever list of
arguments that I want.
Dan
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Well, if you've written your program as a .py file, the usual technique is
to enter an import statement to the interactive interpreter. The code is
executed as the import proceeds. The __name__ == _"__main__" trick is
usually used to run test code in a module intended primarily for import by
other code rather than for execution as a program.
Of course, this only works once, because further imports will assume they
can use teh already-imported module. To get subsequent modifications of the
script to work you have to use reload(mdulename), which will force
execution.
It's a slight pain that the first import is different from all the others,
but never mind.
regards
Steve
Suppose you have, somewhere on the path Python searches, a
file foo.py containing the single Python statement:
print "I'm being executed"
When, at the interactive interpreter prompt, you type an import
statement:
>>> import foo
I'm being executed
>>>
module foo.py has been loaded and, since this was the first time
it was imported in this session, its module-level code was executed.
Another import statement will NOT repeat execution of the module's
code...:
>>> import foo
>>>
No execution in this case, because it was not the first import
in this session!
You can, however, force such re-execution by reloading the
module object after having imported it:
>>> reload(foo)
I'm being executed
<module 'foo' from 'foo.pyc'>
>>>
The reload function also returns the new module-object, which
is why we see this second line here (the interpreter is showing
it to us) -- if you don't want this little bother just assign it:
>>> _=reload(goo)
I'm being executed
>>>
Alex
execfile(script_name)
E.g.,
execfile("test.py")
--
Moshe Zadka <s...@zadka.site.co.il>
This is a signature anti-virus.
Please stop the spread of signature viruses!
Fingerprint: 4BD1 7705 EEC0 260A 7F21 4817 C7FC A636 46D0 1BD6
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 <mik...@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.
-----------------------------------------------------------------------
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:mik...@isy.liu.se]
Sent: Friday, January 19, 2001 6:52 AM
To: Dan Rolander
Cc: pytho...@python.org
Subject: Re: How to run script from interpreter?
I'm afraid that's beyond me. You'll have to wait for someone else to
jump in.
/Mikael
-----------------------------------------------------------------------
E-Mail: Mikael Olofsson <mik...@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: 16:03:21
did you try putting it in sys.argv[1] ?
import sys
sys.argv = ["spam.py", "one", "two", "three"]
execfile("spam.py")
Cheers /F
str = 'bobsmodule'
fp, pathname, description = imp.find_module( modname )
print 'importing module', modname
LoadedModule = imp.load_module( modname, fp, pathname, description)
LoadedModule.Afunctioninbobsmodule(i)
This gives you flexibility if you need it.
or better:
LoadedModule = __import__('bobsmodule')
Cheers /F
Thanks for your help!
Dan
-----Original Message-----
From: Fredrik Lundh [mailto:fre...@effbot.org]
Sent: Friday, January 19, 2001 12:53 PM
To: pytho...@python.org
Subject: Re: How to run script from interpreter?
Cheers /F