Select to view content in your preferred language

pyt - can't get path of Toolbox in new release

879
6
03-05-2014 04:31 AM
RositaBluhm
Emerging Contributor
Hello,

please help me. The Problem has shown with release of 10.2.1 and arcgis upgrade to Python 2.7.5.

Function:

class Toolbox(object):
    def __init__(self):
       sysPaths = sys.path

The result of sysPath is "['C:\\Windows\\system32\\python27.zip', u'c:\\program files (x86)\\arcgis\\desktop10.2\\arcpy', 'C:\\Program Files (x86)\\Python27\\ArcGIS10.2\\Lib', 'C:\\Program Files (x86)\\Python27\\ArcGIS10.2\\DLLs', 'C:\\Program Files (x86)\\Python27\\ArcGIS10.2\\Lib\\lib-tk', 'C:\\Windows\\System32', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.2\\bin', 'C:\\Program Files (x86)\\Python27\\ArcGIS10.2', 'C:\\Program Files (x86)\\Python27\\ArcGIS10.2\\lib\\site-packages', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.2\\arcpy', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.2\\ArcToolbox\\Scripts']" in the new release.

The former release of 10.2.0 shows this result and includes the Folder-path of the python Toolbox in the first position, which I need very much.
[u'C:\\...\\...\\Install', 'C:\\Windows\\system32\\python27.zip', u'c:\\program files (x86)\\arcgis\\desktop10.2\\arcpy', 'C:\\Python27\\ArcGIS10.2\\Lib', 'C:\\Python27\\ArcGIS10.2\\DLLs', 'C:\\Python27\\ArcGIS10.2\\Lib\\lib-tk', 'C:\\Windows\\system32', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.2\\bin', 'C:\\Python27\\ArcGIS10.2', 'C:\\Python27\\ArcGIS10.2\\lib\\site-packages', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.2\\arcpy', 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.2\\ArcToolbox\\Scripts', 'C:\\Python27\\ArcGIS10.2\\lib\\site-packages\\win32', 'C:\\Python27\\ArcGIS10.2\\lib\\site-packages\\win32\\lib', 'C:\\Python27\\ArcGIS10.2\\lib\\site-packages\\Pythonwin']


What other way is there to get the path of the Toolbox??? Please help!

Regards,
Rosita
Tags (2)
0 Kudos
6 Replies
JoshuaChisholm
Frequent Contributor
I'm not sure if this works with python toolbox, but I use it all the time for stand-alone scripts:
import sys
import os
currentScript=sys.argv[0]
scriptDir=os.path.dirname(currentScript)

Good luck!
0 Kudos
RositaBluhm
Emerging Contributor
Unfortunately "sys.argv" returns "['']". So that doesn't help.

Regards,
Rosita
0 Kudos
ShaunWalbridge
Esri Regular Contributor
Rosita,

In my own toolboxes, I have these lines:

local_path = os.path.abspath(os.path.dirname(__file__))
sys.path.insert(0, local_path)


That will get the path of the current file (in this case, the .pyt) and prepend it to Python's system path.

cheers,
Shaun
0 Kudos
RositaBluhm
Emerging Contributor
Thanks, Shaun. That works.

Regards, Rosita
0 Kudos
RositaBluhm
Emerging Contributor
Anyhow, although it works I get an error-message through ArcCatalog which says:

Traceback (most recent call last):
  File "<string>", line 64, in __init__
NameError: global name '__file__' is not defined


Any idea?



Regards,
Rosita
0 Kudos
RositaBluhm
Emerging Contributor
If anyone is interested in the solution, this is how I fixed it:

I added the line with os.path.abspath... into a file "findingpath.pyc". I import the "findingpath" into the pyt-File and call the function that returns the path. It seems that it works properly.

Regards, Rosita
0 Kudos