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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 14, 2017 at 20:26
4
  • 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. Commented Nov 14, 2017 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? Commented Nov 14, 2017 at 22:05
  • Not sure what you mean. This solution, using sys.argv, worked for that problem. Commented 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. Commented Nov 14, 2017 at 23:45

2 Answers 2

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')
answered Nov 14, 2017 at 23:41
1
  • 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/… Commented Nov 15, 2017 at 0:49
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.

answered Nov 14, 2017 at 21:08
3
  • Does eval work in scripts run from modelbuilder? I know input() and raw_input() don't. Commented 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. Commented 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. Commented Nov 14, 2017 at 21:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.