I have a script that reads user-queried values from a file, then writes just those values to a text file. The text file is then further manipulated in ModelBuilder. I want the user to be able to pick the original file. I added a parameter called INPUTFILE to the script, with type equal to "File" and Direction equal to "Input". In the script, that variable is called. This is the first several lines of the script:
import struct
import numpy as np
import matplotlib.pyplot as plt
fid = open(INPUTFILE,'rb')
When i run the model, it fails at the line "fid = open..." and gives this error message: "NameError: name 'INPUTFILE' is not defined"
But it is an input to the script in the model I built. Its a variable, when you run the Model, it is one of the things you set. you set it by browsing to the file you want the script to read.
What do i have to do to get the script to accept the parameter?
-
Where are you using sys.argv[] or arcpy.GetParameter/GetParameterAsText to ingest the script parameter? My personal preference is for sys.argv[] which would seem appropriate as you're not importing arcpy in the snippet. import sys then INPUTFILE = sys.argv[1], that should get you out of trouble.Michael Stimson– Michael Stimson2017年11月14日 21:05:27 +00:00Commented Nov 14, 2017 at 21:05
-
1Ah, there's a trap for beginners. Would you like to answer your own question in your own words for future users with the same (frustrating) problem?Michael Stimson– Michael Stimson2017年11月14日 22:05:28 +00:00Commented Nov 14, 2017 at 22:05
-
Not sure what you mean. This solution, using sys.argv, worked for that problem.user32907– user329072017年11月14日 23:25:01 +00:00Commented Nov 14, 2017 at 23:25
-
The onus on this site is to have an answer for a question, where possible, it's quite OK (encouraged in fact) for the asker (yourself) to answer their own question if the comments lead them to a working solution.. It's also an opportunity to gain some reputation for yourself, we prefer a question be answered because answers are searchable but comments are not. As you're a little hesitant I've put in an answer but feel free to put in your own, after all every question can have many answers.Michael Stimson– Michael Stimson2017年11月14日 23:45:04 +00:00Commented Nov 14, 2017 at 23:45
2 Answers 2
It appears in your script you're not ingesting the variable set on the script parameters. Although when setting up your tool you have added the INPUTFILE as a parameter it doesn't match with an existing sys.argv[], arcpy.GetParameter or arcpy.GetParameterAsText, one of these functions will ingest the parameter of the tool and set the variable.
I would suggest that as you're not importing arcpy to consider using sys.argv[], which is a builtin function:
import struct
import numpy as np
import matplotlib.pyplot as plt
import sys
INPUTFILE = sys.argv[1] # set the variable INPUTFILE with the parameter of the tool
fid = open(INPUTFILE,'rb')
-
got it. thanks! I'll keep in mind the ting about answering the question. For your viewing pleasure, i've posted another question about the same script (new problem): gis.stackexchange.com/questions/261861/…user32907– user329072017年11月15日 00:49:00 +00:00Commented Nov 15, 2017 at 0:49
A way to do this in the script might be:
INPUTFILE = eval('Enter the input file name. ') fid = open(INPUTFILE, 'rb')
You would have to do the first line differently if you want the user to supply a command line argument.
-
Does eval work in scripts run from modelbuilder? I know input() and raw_input() don't.Michael Stimson– Michael Stimson2017年11月14日 21:09:40 +00:00Commented Nov 14, 2017 at 21:09
-
Good question. Sorry, I don't know. It has been a while since I used ModelBuilder and I don't have access to it now.drsnark– drsnark2017年11月14日 21:14:47 +00:00Commented Nov 14, 2017 at 21:14
-
No, I think it won't work. Have a read of stackoverflow.com/questions/9383740/what-does-pythons-eval-do, you would need to include raw_input() in there somewhere and I know for a fact that the raw_input() function doesn't work inside a script tool. Nice try anyway.Michael Stimson– Michael Stimson2017年11月14日 21:49:20 +00:00Commented Nov 14, 2017 at 21:49