Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
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 Feb 26 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 Feb 26 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 Feb 26 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 Feb 26 at 21:30

2 Answers 2

up vote 0 down vote accepted

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

share|improve this answer
    
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 Feb 26 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 script –  Jelle Feb 28 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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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