I have an inline variable substiution which I would like to import into my Python script. Is that possible? I am trying to make a parameter within my path.
My path currently is "c:\Users\Jelle\Dropbox". Within Model Builder I can substitute 'Jelle' by using an inline variable subsitution (name) and call it through %name%. I would like to replace "Jelle" with other names but then within a Python script.
I have tried using GetParameterAsText and making the inline variable as a Parameter within Model Builder but I get an empty output: Here is the script which I tried for it:
import arcpy
username = arcpy.GetParameterAsText(0) #this is the inline variable 'name' which I am trying to call
input_table = "C:\\Users\\"+str(username)+"\\Dropbox"
Input_table gives the following return:
"C:\Users\\Dropbox"
Hence the 'username' variable is not returned
I have the feeling I should be using something else other than GetParameterAsText. Could someone help me out here?
-
I think the issue is that I am not settung the parameter as this is already given as a variable within model builder while parameters within model builder would be used so that they can be adjusted when the model is being setup to run.Jelle– Jelle2015年02月26日 20:46:36 +00:00Commented Feb 26, 2015 at 20:46
-
Are you trying to do this all in Python code or trying to include a Python script tool as part of your model?PolyGeo– PolyGeo ♦2015年02月26日 20:46:38 +00:00Commented Feb 26, 2015 at 20:46
-
I am trying to use Python code within a 'Calculate field' tool within Model Builder. So within the 'Calculate Field' tool I want to call for the inline variables that are present in my model.Jelle– Jelle2015年02月26日 20:51:19 +00:00Commented Feb 26, 2015 at 20:51
-
Can you edit your question to include a simple test model and test script that illustrates the model/code pattern that you are trying to get working? From that I suspect it will be easier to see what you have tried and where you are stuck.PolyGeo– PolyGeo ♦2015年02月26日 21:30:12 +00:00Commented Feb 26, 2015 at 21:30
2 Answers 2
you can use your inline variable directly in the calculateField tool.
with a Python expression entered in the CalculateField tool, it would look like this:
"C:\\Users\\{}\\Dropbox".format(%name%)
The variable should be present in your model (right clic> create variable> Type = String). Here I renamed it "Name" and required it as a precondition, but this should not be necessary. Check this variable as a model parameter if you want users of the model to modify it from the form.
enter image description here
-
This seems like an answer to the right direction! It gets the inline variable ('Jelle') but now gives this error that this global variable is not defined. How would I solve this issue?Jelle– Jelle2015年02月26日 21:49:54 +00:00Commented Feb 26, 2015 at 21:49
-
Thanks for your answer. I figured out what the issue was. If I use the script within my pre-code block, I need to define the variable %Name%. This is what I wrote in my scriptJelle– Jelle2015年02月28日 23:45:37 +00:00Commented Feb 28, 2015 at 23:45
Thanks for your answer. I figured out what the issue was. If I use the script within my pre-code block, I need to define the variable %name%. This is what I wrote in my pre-code block script:
def my_funcrtion():
import arcpy
my_name = "%name%"
fc = "C:/" + my_name + "/Dropbox/featureclass"
Explore related questions
See similar questions with these tags.