954,557 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

execute python script from shell/Interactive Window

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 "", line 1, in ?
NameError: name 'script' is not defined

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 

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

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
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...

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 

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

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 
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 "", 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.

Micko
Junior Poster
148 posts since Aug 2005
Reputation Points: 55
Solved Threads: 6
 

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
vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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

mimens
Newbie Poster
1 post since Feb 2007
Reputation Points: 10
Solved Threads: 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 [email]grigerdrogba@webmail.co.za[/email]
cheers

zovuyo2002
Newbie Poster
1 post since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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

LostGurl
Newbie Poster
3 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

A way to achieve this is the following:
[code]
>>>import sys
>>>sys.argv = ['', 'my', 'args', 'here']
>>>execfile('myscript.py')

zolique
Newbie Poster
1 post since Jun 2010
Reputation Points: 10
Solved Threads: 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 [email]grigerdrogba@webmail.co.za[/email] 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.

danholding
Junior Poster in Training
56 posts since Aug 2010
Reputation Points: 15
Solved Threads: 1
 

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

mithundas
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 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.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

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

danholding
Junior Poster in Training
56 posts since Aug 2010
Reputation Points: 15
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You