How to reload imported modules after editing Win 6 SR21

I have python scripts that import a library script in the same directory that are working as expected. However, when I move a function from a script to the library (so I can use it in the other scripts) Rhino 6 bonks with an error message:

Message: ‘module’ object has no attribute ‘function_name_I_just_moved’

I’m guessing that the library I’ve imported is not getting reloaded after the edit. I’m a *nix Python person, not IronPython, so I’m wondering if I have to force a reload in Rhino.

Yes it may be worth trying to reload(module) and watch out for imports of type from x import y, which may not reload as expected.

Otherwise you may need to restart Rhino.

You could also try from __future__ import absolute_import to get modern behaviour on relative paths.

Reload fixed it:

import imp
import module

No need for imp. reload was a builtin in python 2 so it works without import

Hey there,

just wanted to add a solution I came up with in case the reload() is to restricted. That is the case, for example, if the script you imported is referencing another script (i.e. inheritance) and both are continuously changing during development.

Say you have a file with class A() and a file b.p with a class B(A): and in your script you do:

from b import B
myobj = B()

Here, the reload() will not suffice when changes in file occur and this setting is very common if you develop a whole set of external methods in a well structured way.

Now, here’s the solution. You create a package or namespace (i.e.) a folder with all your files. Say project and reference your files via from project.a import A . A full reset of all modules in that project folder can be achieved by putting the following in your code, before any other imports:

import sys
to_delete = list()
for module in sys.modules:
   if "project" in module:
      to_delete.append(module)  # add them to a list to avoid "dict-size changed during iteration" error

for module in to_delete:

Hope this is useful for anyone.