1

I have got a toolbox full of models to run from one script.

Is there any code that calculates the location of script from which I can get the path to my toolbox?

Script and models are imported if that matters. Toolbox is going to be used on a few computers and running a script is more nice way than searching for models.

  • 1
    Could you please be more specific on what setup do you have? You have a toolbox that contains multiple models. You are writing a Python script that will import this toolbox using arcpy.ImportToolbox() and then run the models as necessary. What do you want to do now? – Alex Tereshenkov Dec 16 '16 at 11:48
  • I want to share this toolbox between collegaues and because of different locations arcpy.ImportToolbox() which was made on my computer will not run on the other one because of another location. I.e. I have arcpy.ImportToolbox(r"D:\Data\Folder\toolbox.tbx") and other user placed in E:\Data\NewFolder\toolbox.tbx. In this case script will give an error. So is that possible to calculate the path to the only script? – Pavel Pereverzev Dec 16 '16 at 11:56
1

Since your script is imported into the toolbox, this will provide the path to the toolbox

pathname = os.path.realpath(__file__)
pathname = pathname.split(".tbx",1)[0]+".tbx"
  • If you're operating on paths, just use os.path.basename(). This will work properly if the path contains .tbx. – Paul Dec 17 '16 at 1:42
  • @Paul I tried both methods you recommended, I had imported script but it gave me a path like C:\Windows\system32\script while toolbox is located in for example D:\Data\Folder\ – Pavel Pereverzev Dec 18 '16 at 22:07
  • @TunaSammich, I got the result like in my comment below – Pavel Pereverzev Dec 19 '16 at 8:12
  • @PavelPereverzev, it's possible you'll need to grab __file__ before you import the toolbox. – Paul Dec 19 '16 at 16:55

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.