Shared project directory path fixup looks wrong

983
5
03-15-2020 07:32 PM
DonMorrison1
Frequent Contributor

I'm packaging a project with an embedded python toolbox with the intent of sharing it with others via ArcGIS Online. The python tool imports a module and it looks like the path to that module is being incorrectly changed during consolidation.

The path to the imported module as I created it is:

E:\ROW_as_habitat\temp\test_dir

The path after the consolidation(I ran Package Product, uploaded the .ppkx to ArcGIS Online, then open the uploaded .ppkx in ArcGIS Online via the portal) is 

C:\Users\DMORRI~1\AppData\Local\Temp\ArcGISProTemp1188\..\test_project_1\p20\test_dir

I would expect the fixed up path to point into the Packages directory, but this is pointing into the temp directory. I do see the module in what I believe is the proper place in under the Packages directory. In any case the import fails when I run the tool from the packaged project. 

Here is the tool code as I created it in ArcGIS Pro

# -*- coding: utf-8 -*-

import arcpy


class Toolbox(object):
    def __init__(self):
        self.label = "Toolbox"
        self.alias = ""
        self.tools = [Tool]


class Tool(object):
    def __init__(self):
        self.label = "Tool"
        self.description = ""
        self.canRunInBackground = False

    def getParameterInfo(self):
        params = None
        return params

    def isLicensed(self):
        return True

    def updateParameters(self, parameters):
        return

    def updateMessages(self, parameters):
        return

    def execute(self, parameters, messages):
        test_dir = r'E:\ROW_as_habitat\temp\test_dir'
        arcpy.AddMessage(test_dir)
        sys.path.append(test_dir)
        import logger as row_logger
        arcpy.AddMessage(row_logger.LOG_FILE)
        return

Here is the code after the fixup

# -*- coding: utf-8 -*-
#
# Esri start of added imports
import sys, os, arcpy
# Esri end of added imports
# Esri start of added variables
g_ESRI_variable_1 = os.path.join(arcpy.env.scriptWorkspace,'..\\test_project_1\\p20\\test_dir')
# Esri end of added variables
# -*- coding: utf-8 -*-
import arcpy

class Toolbox(object):
 def __init__(self):
 self.label = "Toolbox"
 self.alias = ""
 self.tools = [Tool]

class Tool(object):
 def __init__(self):
 self.label = "Tool"
 self.description = ""
 self.canRunInBackground = False
def getParameterInfo(self):
 params = None
 return params
def isLicensed(self):
 return True
def updateParameters(self, parameters):
 return
def updateMessages(self, parameters):
 return
def execute(self, parameters, messages):
 test_dir = g_ESRI_variable_1
 arcpy.AddMessage(test_dir)
 sys.path.append(test_dir)
 import logger as row_logger
 arcpy.AddMessage(row_logger.LOG_FILE)
 return
5 Replies
by Anonymous User
Not applicable

What if you coded that path variable to be relative to the where the script file is, if that is possible?

Not entirely sure but to me it looks like arc is being smart enough to change the path into an arcgis created temp directory in case a user doesn't have an E:? - like expected, but then it is not finding the module- which is in a different older at this point? 

Could you hard code in the relative path to the module to import?

I don't know really... Just thinking through some ideas and would like to see what the solution comes to be.

0 Kudos
DonMorrison1
Frequent Contributor

Thanks Jeff - I'll give that a try in the next couple of days.  I've been able to package and share Python toolboxes OK. With the toolboxes, the referenced directory ends up getting installed in the user's Documents\ArcGIS\Packages directory and the paths in the code get fixed up correctly to point there.  But now that I'm trying to package the entire project (with the toolbox inside the project) the referenced directory is still ending up in the Documents\ArcGIS\Packages directory as I would expect but the code is altered to point to the Temp directory.  I'll see if a relative path works any better and let you know.

0 Kudos
DonMorrison1
Frequent Contributor

I finally got back to this and tried the relative path. That didn't work either.

The relative path of test_dir = os.path.normpath(__file__ + '/../../temp/test_project')

Got changed to C:\Users\dmorrison\Documents\ArcGIS\Packages\packaged_a1e8f8\p20\p_toolbox.pytC:\Users\DMORRI~1\AppData\Local\Temp\packaged\p20\test_project

For some reason it always changes the path to point in the user's temp directory instead of the user's package directory (where the files have been properly consolidated)

0 Kudos
DonMorrison1
Frequent Contributor

It appeared that maybe this was fixed in ArcGIS Pro 2.6.0 (BUG-000116650) but further testing shows that it the generated path is still incorrect. It looks like ESRI may have tried to fix it because the generated path is different, but it is still incorrect.

Here is the path as coded in the toolbox .pyt file that is included in the project 

IMPORT_DIR = r'C:\Git_Repository\ROW_as_habitat\row\consolidation'

Here is how it shows up after consolidation

g_ESRI_variable_1 = os.path.join(arcpy.env.scriptWorkspace,'consolidation')

Which resolves to this directory once the project is open via the AGOL portal

C:\Users\DMORRI~1\AppData\Local\Temp\ArcGISProTemp16060\consolidation

 This is where it actually exists in the downloaded package

C:\Users\dmorrison\Documents\ArcGIS\Packages\TEST_PROJECT_541a08\p20\consolidation

0 Kudos
TorbjørnDalløkken2
Frequent Contributor

Just wanted to say that I get the same kind of error in ArcGIS Pro 3.0.3 as well. I have a python toolbox with several python scripts in this folder structure (the pyt references the scripts in the Scripts folder):

- OwnershipCalculation
-- OvershipCalculation.pyt
-- Scripts
---- Calculation.py
---- ExportResults.py

When consolidating the toolbox, the Scripts folder is put inside a new folder with the same name as the parent folder:

- OwnershipCalculation
-- OvershipCalculation.pyt
-- OwnershipCalculation
----Scripts
------ Calculation.py
------ ExportResults.py

While the .pyt still references the old path.

0 Kudos