1,105,534 Community Members

execute python script from shell/Interactive Window

Member Avatar
Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 2 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

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):

import sys

print 'Begining... now...'

for x in sys.argv:
    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

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

at the >>> prompt type the following:
execfile("script.py")
and press enter

Member Avatar
Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 2 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

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...

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Try this:
execfile("script.py argument1 argument2")
For the moment I don't have access to Python, let me know if it works.

Member Avatar
Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 2 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Found this in my code library ...

# run an external program from within Python code
# subprocess.call(["program-name", "param1", "param2"])
# safer than   os.system("program-name")
# new in Python24

import subprocess
subprocess.call(["D:/Python24/Python.exe", "Skripta.py", "arg1", "arg2"])

Where Skripta.py contains this code ...

import sys
import time

print 'Beginning... now...'

for x in sys.argv:
    print x
    
time.sleep(5)  # 5 seconds to look at result
Question Answered as of 7 Years Ago by vegaseat
Member Avatar
mimens
Newbie Poster
1 post since Feb 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
zovuyo2002
Newbie Poster
1 post since Oct 2007
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
LostGurl
Newbie Poster
3 posts since Mar 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi..im using MontyLingua in Python..anybody knows how to call and use MontyTagger? Pls provide me some coding example..

Member Avatar
zolique
Newbie Poster
1 post since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

A way to achieve this is the following:

>>>import sys
>>>sys.argv = ['', 'my', 'args', 'here']
>>>execfile('myscript.py')
Member Avatar
danholding
Junior Poster in Training
56 posts since Aug 2010
Reputation Points: 5 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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

hi there mate if you are still after a tutorial i found the best one to use was on youtube by "thenewboston" just search for him on youtube i managed to learn most of the basic functions in 2hours from the tutorials.

Member Avatar
mithundas
Newbie Poster
1 post since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

how to import an existing python file and run it in an python shell....

Member Avatar
Gribouillis
Posting Maven
3,454 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 884 [?]
Skill Endorsements: 18 [?]
Moderator
 
0
 

how to import an existing python file and run it in an python shell....

Start by reading this http://docs.python.org/tutorial/modules.html . Also you can start a new thread if you have more questions.

Member Avatar
danholding
Junior Poster in Training
56 posts since Aug 2010
Reputation Points: 5 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
1
 

also 'thenewboston' tutorials on python explains and shows an example of this

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article