run python script in python window

19319
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 think Michael's original question was that if he has a script like so:

import sys
x = sys.argv[1]
y = sys.argv[2]
print x + y

How can the script be executed in the ArcGIS Python Window so that a user can specify the arguments for x and y? There doesn't seem to be a way for the python window to accept user input using the standard sys.argv[] method (like via Pythonwin)

Correct me if I am wrong, but it seems like the Python window is basically just an interactive window (not a full blown IDE), and not really geared for running a full script in (just one line at a time).
0 Kudos
RDHarles
Occasional Contributor
Here's one approach:

Run the script from ARC 10 like this:
>>> import os
>>> os.system("myscript.py  Hi  Mom")

myscript.py looks like this and will write the 2 arguments to log.txt:
import sys
one = sys.argv[1]
two = sys.argv[2]
message = open("c:/temp/log.txt", "a")
message.write(one+"\n")
message.write(two+"\n")
** If you want to run python scripts (on a regular basis) from the command line though, it's easier to just pull up a windows command prompt and run it from there.
If your system is set up right, for the command prompt, you can simply do:
> myscript  Hi  Mom
DavidWynne
Esri Contributor
Yes, I think Charles has a way of running that script 'as is'.  Although for the original case, the arguments will be coming in as strings, so if the inputs are 1 and 2, the answer will be 12 (adding "1" to "2") vs 3--which could be accomplished by something like x = int(sys.argv[1])

Correct me if I am wrong, but it seems like the Python window is basically just an interactive window (not a full blown IDE), and not really geared for running a full script in (just one line at a time).


It's all about how you organize your code.  The more 'pythonic' way would be to organize the code into function(s), which is very easy to just call, and without having to run the script as a file. 

-Dave
0 Kudos
MikeTischler
New Contributor II
Charles' solution works for me - at least for this particular problem.  I'm moving from VBA/.NET ArcObjects into python, so it'll take me a while to be "pythonic". 

Thanks guys,
mike
0 Kudos
LT
by
Occasional Contributor
Hi all,

I'm just wondering if there's been some update on this problem yet.  sys.argv won't work.   I tried using  SetParameterAsText and GetParameterAsText together too.  That didn't work.  

Thanks!
0 Kudos
RobertBurke
Esri Contributor
I just wanted to update this thread with a test I ran today in ArcMap 10.2, attempting to use GetParameterAsText.

I first made an empty text file in my temp folder, c:\temp\log.txt (no programming, just went in and made the text file.)

Here is the script I used to test, it builds off the one shown earlier/above in this discussion.

import sys
import os
import arcpy

#one = sys.argv[1]
#two = sys.argv[2]

one = arcpy.GetParameterAsText(0)
two = arcpy.GetParameterAsText(1)

message = open("c:/temp/log.txt", "a")
message.write(one+"\n")
message.write(two+"\n")

Here is what I did in the Python Window in ArcMap, it ran and returned exit code 0.

>>> import os
>>> os.system("c:\student\script1.py hi mom")
0
>>>


Here are the contents of the output text file, log.txt.

hi
mom
JoeBryant1
Occasional Contributor III

Thanks for the helpful update, Robert!

If you were planning on executing the script from Windows Task Scheduler, would you move the comments hash from the sys.argv variables and put them in front of the arcpy.GetParameterAsText variables in your script, or are you just explaining where those variable will receive their arguments from?

In other words, do we need to use sys.argv for Scheduled Tasks?

0 Kudos