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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 16, 2016 at 11:05
2
  • 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? Commented Dec 16, 2016 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? Commented Dec 16, 2016 at 11:56

1 Answer 1

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"
answered Dec 16, 2016 at 14:30
4
  • If you're operating on paths, just use os.path.basename(). This will work properly if the path contains .tbx. Commented Dec 17, 2016 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\ Commented Dec 18, 2016 at 22:07
  • @TunaSammich, I got the result like in my comment below Commented Dec 19, 2016 at 8:12
  • @PavelPereverzev, it's possible you'll need to grab __file__ before you import the toolbox. Commented Dec 19, 2016 at 16:55

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.