I am finding myself a little confused about how Python imports. Would the following be a correct statement:

In any Python interactive shell, if the shell (since it was initiated or opened) has never before imported "myScript.py" then if the user types in "myScript" he will get an error like: NameError, name 'myScript' is not defined. At this point, entering the command "import myScript" will import myScript.py from the native OS diskfile system, i.e. from Windows folder or Unix directory, based upon a search path directed by "sys.path". Even if "myScript" is defined or has been imported in some other currently existing Python shell, the shell we are currently in will import from disk and not from the other shell.

The reason I ask is, the Blender 3D application I am working with comes with a built in Python shell. Blender can run continuously while the user chooses to open, close, re-open the built in Python shell. If, after importing "myScript" into a shell, I close the shell, make some changes to myScript.py through an external editor, and re-open a new Python shell, the definition for "myScript" is no longer there (NameError: name 'myScript' is not defined). But when i import myScript, it imports the old version, so apparently Blender has saved the old version somewhere and is allowing the Python shell to reload it. This violates Python rule that if not currently defined in namespace, import myScript should always import from the native OS file system, never from some previously loaded module, correct?

There is no such rule. import myScript reads the file on the disk if there is no module in sys.modules['myScript']. This module may exist even if the name myScript is not defined in the current global namespace. Apparently, your app closes and reopens the shell window in the same python process. One thing you could do is

import myScript
reload(myScript)

if there is no module in sys.modules['myScript']. This module may exist even if the name myScript is not defined in the current global namespace.

Thank you! Exactly what I needed to know! So closing and re-opening the shell loses (or may lose) the definition but does not clear or delete from sys.modules. It seems a bit counter-intuitive but explains everything. Thank you "Gribouillis"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.