run python script in python window

19130
16
08-11-2010 07:55 AM
MikeTischler
New Contributor II
Hi,
This seems absurdly simple to ask, but I cannot seem to find the answer, and I've scoured the help files.

I have a script (let's say it's called myscript.py) and requires 2 arguments. 

How the heck do I execute that script from within the python window in ArcGIS 10?

execfile() seems close, but I can't figure out how to pass the arguments. 

Thanks,
mike
0 Kudos
16 Replies
ChrisSnyder
Regular Contributor III
I haven't messed with it much, but I don't think there is a way of passing arguments to a script when you execute it via the python window! If I am right, ESRI must have overlooked this one. I think your options would then be to:

1. Hard code the arguments in the script, and run it via the python window.
2. If your arguments are simple, like a  single number or something, use the input() method. Example: test = input("Provide a number")
3. Run the script in Pythonwin, wing, etc. where you can specify the parameters.
4. Build a toolbox and use arcpy.getparameterastext() in you code to accept the arguments from the toolbox.
0 Kudos
MikeTischler
New Contributor II
Thanks Chris,
Those workarounds do just fine - but it seems silly to have to do those.  Isn't passing arguments a very, very basic functionality?
0 Kudos
ChrisSnyder
Regular Contributor III
You would think...
0 Kudos
JasonScheirer
Occasional Contributor III
Typically you'd set it up to behave like a module:

def mainfunc(a, b):
  return do_some_stuff(a, b)

if __name__ == "__main__":
  import sys
  mainfunc(sys.argv[1], sys.argv[2])


Then you can call mainfunc(a, b) in the Python window once you've loaded it in. The interactive console's environment is slightly different than running a standalone script, just like in any other Python prompt (Pythonwin, Idle, etc).
0 Kudos
ChrisSnyder
Regular Contributor III
But how do you specify values for sys.argv[whatever] via the Python window in ArcMap? It seems as if you can't...
0 Kudos
ChrisSnyder
Regular Contributor III
But through what dialog in the Python window can a users specify values for sys.argv[whataver]?

>>> def func(a,b):
...    print str(a) + " - " + str(b)
...
>>> func(1,2)
1 - 2

>>> import sys
>>> func(sys.argv[1],sys.argv[2])
Runtime error <type 'exceptions.IndexError'>: list index out of range
0 Kudos
JasonScheirer
Occasional Contributor III
You would do something like this:

a = "c:\\data\\my.gdb\\featureclass"
b = 5
func(a, b)


No need to get sys.argv involved.
0 Kudos
MikeTischler
New Contributor II
and what is the syntax for calling a .py script ?  I can't just type the name of the python file into the command line.
0 Kudos
DavidWynne
Esri Contributor
and what is the syntax for calling a .py script ?  I can't just type the name of the python file into the command line.


I have used methods like os.execl, etc. (see more on http://docs.python.org/library/os.html) to call a file directly (including .py's) with varying levels of success.  But that's not going to run in process or anything, and passing in arguments like a layer in your current map isn't going to work.  Jason's approach will be the cleanest way to do it.

-Dave
0 Kudos