execute python script from shell/Interactive Window

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

execute python script from shell/Interactive Window

 
0
  #1
Aug 24th, 2005
Hello,
I'm using PythonWin and I have a problem.
i don't know how to run script from Python shell Pythonwin Interactive Window).
For example I wrote this code (from tutorial):
  1. import sys
  2.  
  3. print 'Begining... now...'
  4.  
  5. for x in sys.argv:
  6. print x

and I save it as Script.py in Folder Atest which is in Python24
However, I have a problem to run it with command line arguments.
I can do that by choosing File->Run and then to enter arguments,
but I'd like to find out how to to run script from shell.
At first, I thought the problem is because there is no defined path to folder Atest, but I saved file in Python24 folder but problem still exists.
I have been trying to execute script.py by typing
>>> script.py and keep getting the following error:
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
NameError: name 'script' is not defined
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,145
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 949
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: execute python script from shell/Interactive Window

 
0
  #2
Aug 24th, 2005
at the >>> prompt type the following:
execfile("script.py")
and press enter
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: execute python script from shell/Interactive Window

 
0
  #3
Aug 24th, 2005
Originally Posted by vegaseat
at the >>> prompt type the following:
execfile("script.py")
and press enter
Thanks for the answer, but I still have problems, I can't figure out how to use it with arguments...
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,145
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 949
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: execute python script from shell/Interactive Window

 
0
  #4
Aug 24th, 2005
Try this:
execfile("script.py argument1 argument2")
For the moment I don't have access to Python, let me know if it works.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 148
Reputation: Micko is on a distinguished road 
Solved Threads: 6
Micko Micko is offline Offline
Junior Poster

Re: execute python script from shell/Interactive Window

 
0
  #5
Aug 25th, 2005
Originally Posted by vegaseat
Try this:
execfile("script.py argument1 argument2")
For the moment I don't have access to Python, let me know if it works.
I've tried that almost immediately after I saw that execfile() function. Unfortunately I'm getting message:
execfile("skripta.py arg1 arg2")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
IOError: [Errno 2] No such file or directory: 'skripta.py arg1 arg2'

(File is named skripta.py so this isn't mistake)

I found in documentation this
"execfile(...)
execfile(filename[, globals[, locals]])

Read and execute a Python script from a file.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it."

So probably execfile doesn't support args....

Thank you vegaseat, I'm very grateful for your help.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,145
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 949
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: execute python script from shell/Interactive Window

 
0
  #6
Aug 25th, 2005
Found this in my code library ...
  1. # run an external program from within Python code
  2. # subprocess.call(["program-name", "param1", "param2"])
  3. # safer than os.system("program-name")
  4. # new in Python24
  5.  
  6. import subprocess
  7. subprocess.call(["D:/Python24/Python.exe", "Skripta.py", "arg1", "arg2"])
Where Skripta.py contains this code ...
  1. import sys
  2. import time
  3.  
  4. print 'Beginning... now...'
  5.  
  6. for x in sys.argv:
  7. print x
  8.  
  9. time.sleep(5) # 5 seconds to look at result
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1
Reputation: mimens is an unknown quantity at this point 
Solved Threads: 0
mimens mimens is offline Offline
Newbie Poster

Re: execute python script from shell/Interactive Window

 
0
  #7
Feb 9th, 2007
Originally Posted by Micko View Post
I've tried that almost immediately after I saw that execfile() function. Unfortunately I'm getting message:
execfile("skripta.py arg1 arg2")
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
IOError: [Errno 2] No such file or directory: 'skripta.py arg1 arg2'

(File is named skripta.py so this isn't mistake)

I found in documentation this
"execfile(...)
execfile(filename[, globals[, locals]])

Read and execute a Python script from a file.
The globals and locals are dictionaries, defaulting to the current
globals and locals. If only globals is given, locals defaults to it."

So probably execfile doesn't support args....

Thank you vegaseat, I'm very grateful for your help.
Hello,

I met the same problem and here is the solution that I found:

execfile(filename)
functionname(argument)

Cheers.
m.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1
Reputation: zovuyo2002 is an unknown quantity at this point 
Solved Threads: 0
zovuyo2002 zovuyo2002 is offline Offline
Newbie Poster

Re: execute python script from shell/Interactive Window

 
0
  #8
Jan 31st, 2008
guys im still a newbie in programming I also had problem in running this small things im doing in python ,so if its possible can u plz send for me the best tutorial link to this email add grigerdrogba@webmail.co.za
cheers
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 48393 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC