ArcGIS Pro bug?: Can't import custom Python module in "Calculate Value" tool of ModelBuilder

5023
25
03-29-2018 02:23 PM
MarcoBoeringa
MVP Regular Contributor

NOTE: This post is NOT about pure Python toolboxes, but ordinary ModelBuilder toolboxes enhanced with Python scripts.

Can ESRI confirm this? I am running into an issue where it seems Pro won't import custom Python modules / scripts in a Calculate Value tool of ModelBuilder. I can successfully import default ArcGIS Python modules like io etc, just not my own modules written as scripts. Pro bails out with a "ModuleNotFoundError".

To avoid confusion, with "import", I mean using a Python module import statement in the Python code textbox of the Calculate Value tools, so like:

import os

import MyModule

This doesn't happen in ArcMap at all. Even if the module is not imported in the toolbox, ArcMap will find it if it is located in the same folder as the toolbox. Pro fails both with a script in the same folder as the toolbox, as well as with imported scripts.

See the attached screenshot from Pro 2.1.2.

0 Kudos
25 Replies
MarcoBoeringa
MVP Regular Contributor

Shaun,

Please see my last responses to Curtis, and especially the conclusion that Pro mangles Folder type script parameters by replacing spaces with underscores: https://community.esri.com/message/760846-re-arcgis-pro-bug-cant-import-custom-python-module-in-calc... 

Of course, there remains the main issue that Pro fails to import the custom Python module in the first place, unless the sys.path is explicitly set as per Curtis suggestion, but at least I now have a working solution.

0 Kudos
DanPatterson_Retired
MVP Emeritus

So the space in folder names extends beyond recent issues

https://community.esri.com/message/755245-spaces-in-portal-folder-names

And Curtis Price‌ 's blog post... How to name things... which his 'snarky' comment and my 'space' comment needs to be updated for Pro.

Kory Kramer‌ you can add this issue to the list.  Maybe it could be added to checks when validating.

0 Kudos
MarcoBoeringa
MVP Regular Contributor

Dan,

Even if ESRI considers spaces for any folder name involved in GIS a big no-no, then the application should throw some exception with a clear message, instead of auto-converting the folder's path stored in a geoprocessing parameter to an invalid one with an underscore... that operation is just senseless.

By the way, actually much if not most of Pro seems quite happy handling spaces in folder names. I have had few problems with that, except this one I am encountering now.

0 Kudos
DanPatterson_Retired
MVP Emeritus

It is a know issue, and that is the gist of the conversation... and it is long standing... see the update to my thread.

It could have been worse... everything could have be in the c:\Users.... folder which is the bane of those of us who teach

0 Kudos
curtvprice
MVP Esteemed Contributor

Space handling is getting better over the years but I still don't recommend using them if they can be avoided. Things work until you get to a point in your process and need to run a raster tool or join and there you are. Spaces within scripts of course, we need to deal with it because our software is in Program Files and the Pro python sys.prefix even has a space in it. 

I thought of a better way to get that path that doesn't require adding a dummy script! But it does require a file in your .tbx folder -- there is usually something there like a readme or .lyr file there anyway, so that's not a big hassle. The parse path tool outputs a string so it does not try to validate it (no spaces converted to _).

I did tried adding the tbx file itself instead of 00readme.txt because that would be cool, but it (understandably) confused Pro (crash). The screenshot below is ArcMap but I tested in Pro too.

Remember to set relative paths for the model tool so the path on 00readme.txt is correct.

model builder get toolbox path v2

In Pro, Calculate Value returned:

Value = ['\\\\Mac\\Home\\Documents\\ArcGIS\\Projects\\MyProject\\testing - Copy', 'c:\\program files\\arcgis\\pro\\Resources\\arcpy', 'C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\DLLs', 'C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib', 'C:\\Program Files\\ArcGIS\\Pro\\bin', 'C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3', 'C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages', 'C:\\Program Files\\ArcGIS\\Pro\\bin', 'C:\\Program Files\\ArcGIS\\Pro\\Resources\\ArcPy', 'C:\\Program Files\\ArcGIS\\Pro\\Resources\\ArcToolbox\\Scripts', 'C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\pytz-2017.2-py3.6.egg', 'C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\lib\\site-packages\\sympy-1.1.1-py3.6.egg', 'c:\\program files\\arcgis\\pro\\Resources\\ArcToolbox\\Scripts', 'c:\\program files\\arcgis\\pro\\bin']

I discovered every time I ran the model it inserted another item in the sys.path (duplicating) so you may want to do this:

tbxpath = r"%tbx folder%"
if sys.path[0] != tbxpath:
  sys.path.insert(0, tbxpath)‍‍‍
0 Kudos
KoryKramer
Esri Community Moderator

This is on that dev team's list.  Thanks, Dan.

0 Kudos