1

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 Nov 14 '17 at 21:05
  • 1
    Ah, 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 Nov 14 '17 at 22:05
  • Not sure what you mean. This solution, using sys.argv, worked for that problem. – user32907 Nov 14 '17 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 Nov 14 '17 at 23:45
1

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')
0

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 Nov 14 '17 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 Nov 14 '17 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 Nov 14 '17 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.