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

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

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

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

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.

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

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.

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..im using MontyLingua in Python..anybody knows how to call and use MontyTagger? Pls provide me some coding example..

A way to achieve this is the following:

>>>import sys
>>>sys.argv = ['', 'my', 'args', 'here']
>>>execfile('myscript.py')

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.

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

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

commented: good reference +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.