Changes in imported arcPy modules

1594
4
09-03-2016 05:33 AM
MatthiasBuehler
Occasional Contributor III

Hi,

when using imported modules and one of the modules changes, ArcGIS PRO does not reflect that change, but runs the old version. it seems to cache it somehow.

the only way to get the changes to run is to restart PRO.

py script 1 that is actually run via toolbox:

import myFile

myFile.runCode()

py script 2 that is imported:

def runCode():
   import arcpy
    arcpy.AddMessage('change me!')

 

Is there a way around this?

Any input welcome!

Matt

0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus

You had better check out some of my blog posts dealing with python... especially the transition from 2.7 to 3.4/3.5

>>> import importlib
>>> dir(importlib)
['_RELOADING', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__import__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_bootstrap', '_bootstrap_external', '_imp', '_r_long', '_w_long', 'abc', 'find_loader', 'import_module', 'invalidate_caches', 'machinery', 'reload', 'sys', 'types', 'util', 'warnings']

>>> help(importlib.reload)
Help on function reload in module importlib:
reload(module)
 Reload the module and return it.
 
 The module must have been successfully imported before.
0 Kudos
MatthiasBuehler
Occasional Contributor III

oh boy.

got it working:

import importlib

cd = importlib.import_module('code')
importlib.reload(cd)

cd.runCode()

thanks for the inputs.. much appreciated!

matt

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I am glad you got it working.  I highly recommend you read the documentation on importlib.reload() .  As powerful and handy as reload() is, there is some fine print with its use.  Without understanding the nuances and caveats of using it, you could get yourself wrapped around an axle at some point.

0 Kudos
DanPatterson_Retired
MVP Emeritus

As Joshua point out, do read the fine print.

I generally do the reload from the command line should I need to edit a script that has been imported.

Should you feel the need to include it in a script, you must remember to comment the reload line(s) before your final commit.

It is also a simpler idea to simply delete references to the imported module(script) and its imports while you are editing..

del my_imported_module

In fact, get used to using your builtins... in this example I cleaned out the 'fluff', found what is currently been loaded, imported a module (aka script), deleted it then examined if it was still around

>>> locals().keys()
dict_keys(['__builtins__'])
>>> import find
>>> locals().keys()
dict_keys(['find', '__builtins__'])
>>> dir(find)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
 '__name__', '__package__', '__private__', '__spec__', '_demo', '_func',
 '_large', 'dedent', 'find', 'ft', 'np', 'script', 'sys']
>>> 
>>> del find
>>> locals().keys()
dict_keys(['__builtins__'])
>>> ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

So, you really don't need any form of reloading, if you understand the command line.

0 Kudos