Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python refresh/reload

This is a very basic question - but I haven't been able to find an answer by searching online.

I am using python to control ArcGIS, and I have a simple python script, that calls some pre-written code.

However, when I make a change to the pre-written code, it does not appear to result in any change. I import this module, and have tried refreshing it, but nothing happens.

I've even moved the file it calls to another location, and the script still works fine. One thing I did yesterday was I added the folder where all my python files are to the sys path (using sys.append('path') ), and I wonder if that made a difference.

Thanks in advance, and sorry for the sloppy terminology.

like image 743
womble Avatar asked Oct 04 '09 18:10

womble


People also ask

What is the use reload () in Python?

Python Programming The reload() is used to reload a previously imported module or loaded module. This comes handy in a situation where you repeatedly run a test script during an interactive session, it always uses the first version of the modules we are developing, even we have made changes to the code.

What is Importlib reload?

When reload() is executed: Python module's code is recompiled and the module-level code re-executed, defining a new set of objects which are bound to names in the module's dictionary by reusing the loader which originally loaded the module.

How do you reload in Python 3?

In Python 3, reload was moved from builtins to imp. So to use reload in Python 3, you'd have to write imp. reload(moduleName) and not just reload(moduleName).


1 Answers

It's unclear what you mean with "refresh", but the normal behavior of Python is that you need to restart the software for it to take a new look on a Python module and reread it.

If your changes isn't taken care of even after restart, then this is due to one of two errors:

  1. The timestamp on the pyc-file is incorrect and some time in the future.
  2. You are actually editing the wrong file.

You can with reload re-read a file even without restarting the software with the reload() command. Note that any variable pointing to anything in the module will need to get reimported after the reload. Something like this:

import themodule from themodule import AClass  reload(themodule) from themodule import AClass 
like image 112
Lennart Regebro Avatar answered Sep 22 '22 17:09

Lennart Regebro