4

Is there a way (enviromental variable) in arcpy to get access to ArcGIS Desktop AppData (per user) folder. For example, I need to access this folder in 10.2:

C:\Users\MyUserName\AppData\Roaming\ESRI\Desktop10.2

Actually, from there I need to access "My Toolboxes" folder!

The reason is that When I upgrade my ArcGIS, I have to change all of my scripts to reflect the new path. Furthermore, if I run the script in another machine the script's path won't be valid.

I need to access this folder in a neat way and I don't want to use an absolute path. As this is a famous folder across the ArcGIS system, I expect an environmental variable for this folder or "My Toolboxes" Folder. Any thoughts?

  • How do you want to access the folder? What do you hope to accomplish? – Aaron Jul 15 '15 at 20:21
  • I need to access to this folder in a clean way. I don't want to use an absolute path. As this is a famous folder across the ArcGIS System, I expect an environmental variable for this folder or My Toolboxes Folder. – Farid Cheraghi Jul 15 '15 at 20:23
  • What is the problem with an absolute path? I am unclear what you ultimately want to accomplish. – Aaron Jul 15 '15 at 20:25
  • 1
    When I upgrade my ArcGIS then I have to change all of my scripts to reflect the new path. Furthermore if I run the script in another machine the script the path won't be valid. I think I should have generate the path via python. check the version of ArcGIS and the current OS user and the build the path! – Farid Cheraghi Jul 15 '15 at 20:31
  • The ultimate goal is to use arcpy.ImportToolbox() method – Farid Cheraghi Jul 15 '15 at 20:49
10

A handy hint for 'special' folders is os.environ.get

Some environments that will help you on windows:

AGSDESKTOPJAVA
ALLUSERSPROFILE
APPDATA
COMPUTERNAME
HOMEDRIVE
HOMEPATH
LOCALAPPDATA
NUMBER_OF_PROCESSORS
PROCESSOR_ARCHITECTURE
ProgramFiles
ProgramFiles(x86)
PUBLIC
SystemDrive
SystemRoot (don't write here, but it's handy for reading)
TEMP
TMP
USERNAME
USERPROFILE
windir

To see a list open a CMD window and type set and press enter, a list will be printed of all environment variables and their values for your local computer.

Your path to toolboxes is aided by arcpy.GetInstallInfo():

"{0}\\ESRI\\Desktop{1}\\ArcToolbox\\My Toolboxes".format(os.environ.get("APPDATA"),arcpy.GetInstallInfo()["Version"])

As a one liner, broken up (for readability):

AppData = os.environ.get("APPDATA") # not case sensitive
II      = arcpy.GetInstallInfo()
Version = II["Version"]             # Case sensitive
TbxFld  = "{0}\\ESRI\\Desktop{1}\\ArcToolbox\\My Toolboxes".format(AppData,Version)

As noted in the comments there is a bug in some versions with the GetInstallInfo()[Version] relating to sub-versions, to avoid this one way would be to:

if len(arcpy.GetInstallInfo()["Version"].split('.')) > 2:
  vString = '.'.join(arcpy.GetInstallInfo()["Version"].split('.')[:-1])
else:
  vString = arcpy.GetInstallInfo()["Version"]
  • 1
    Michael, This is great. Once merged with my own answer, it will become a complete answer. If the user need to access Application Data folder, then your solution is the fit. To access ArcGIS famous folders, as ArcGIS is aware of these pathes (e.g. "My Toolboxes") a simple string (e.g. 'Toolboxes\My Toolboxes\Customtoolbox.tbx') is enough. – Farid Cheraghi Jul 15 '15 at 22:30
  • 1
    Yes it is, in this case a simple string is enough, the intent in this answer is to be general help to find 'special' folders that always exist in windows but may not be in the same place... UserName and ComputerName I use a lot for writing log files onto the server to distinguish where scripts are being run, by whom and if they succeed or fail. Look at the GetInstallInfo() returned dictionary, there's a few priceless gems in there... especially writing scripts to be shared over windows XP, 7 and 8 (32 & 64bit) and ArcGis 10.0 to 10.3. – Michael Stimson Jul 15 '15 at 22:45
  • 1
    arcpy.GetInstallInfo() is great. However there is a bug with your code. if the version is 10.2.1, II["Version"] will return 10.2.1. although the minor releases aren't reflected in folders (C:\Users\Farid\AppData\Roaming\ESRI\Desktop10.2). So you should remove the last 1 from the version string – Farid Cheraghi Jul 15 '15 at 22:50
  • 1
    Something like arcpy.GetInstallInfo()["Version"][:4] – Farid Cheraghi Jul 15 '15 at 22:52
  • 1
    '.'.join( arcpy.GetInstallInfo()["Version"].split('.')[:-1]) by breaking up into a list then re-joining less the last element, but only if len(arcpy.GetInstallInfo()["Version"].split('.')) > 2 – Michael Stimson Jul 15 '15 at 22:55
3

What I was looking for is:

arcpy.ImportToolbox(u'Toolboxes\\My Toolboxes\\Customtoolbox.tbx')

I keep my all toolboxes in this folder (Toolboxes\My Toolboxes).

When I need to access one of my custom GP tool from another toolbox I have to import that toolbox.

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.