python refresh/reload

后端 未结 7 907
北海茫月
北海茫月 2020-11-27 17:58

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 scrip

相关标签:
7条回答
  • 2020-11-27 18:21

    One way to do this is to call reload.

    Example: Here is the contents of foo.py:

    def bar():
        return 1
    

    In an interactive session, I can do:

    >>> import foo
    >>> foo.bar()
    1
    

    Then in another window, I can change foo.py to:

    def bar():
        return "Hello"
    

    Back in the interactive session, calling foo.bar() still returns 1, until I do:

    >>> reload(foo)
    <module 'foo' from 'foo.py'>
    >>> foo.bar()
    'Hello'
    

    Calling reload is one way to ensure that your module is up-to-date even if the file on disk has changed. It's not necessarily the most efficient (you might be better off checking the last modification time on the file or using something like pyinotify before you reload), but it's certainly quick to implement.

    One reason that Python doesn't read from the source module every time is that loading a module is (relatively) expensive -- what if you had a 300kb module and you were just using a single constant from the file? Python loads a module once and keeps it in memory, until you reload it.

    0 讨论(0)
  • 2020-11-27 18:28

    The cases will be different for different versions of python. Following shows an example of python 3.4 version or above:

    hello import hello_world
    #Calls hello_world function
    hello_world()
    HI !!
    #Now changes are done and reload option is needed
    import importlib
    importlib.reload(hello)
    hello_world()
    How are you?
    

    For earlier python versions like 2.x, use inbuilt reload function as stated above. Better is to use ipython3 as it provides autoreload feature.

    0 讨论(0)
  • 2020-11-27 18:31

    I used the following when importing all objects from within a module to ensure web2py was using my current code:

    import buttons
    import table
    reload(buttons)
    reload(table)
    from buttons import *
    from table import *
    
    0 讨论(0)
  • 2020-11-27 18:36

    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
    
    0 讨论(0)
  • 2020-11-27 18:44

    I'm not really sure that is what you mean, so don't hesitate to correct me. You are importing a module - let's call it mymodule.py - in your program, but when you change its contents, you don't see the difference?

    Python will not look for changes in mymodule.py each time it is used, it will load it a first time, compile it to bytecode and keep it internally. It will normally also save the compiled bytecode (mymodule.pyc). The next time you will start your program, it will check if mymodule.py is more recent than mymodule.pyc, and recompile it if necessary.

    If you need to, you can reload the module explicitly:

    import mymodule
    
    [... some code ...]
    
    if userAskedForRefresh:
        reload(mymodule)
    

    Of course, it is more complicated than that and you may have side-effects depending on what you do with your program regarding the other module, for example if variables depends on classes defined in mymodule.

    Alternatively, you could use the execfile function (or exec(), eval(), compile())

    0 讨论(0)
  • 2020-11-27 18:46

    I had the exact same issue creating a geoprocessing script for ArcGIS 10.2. I had a python toolbox script, a tool script and then a common script. I have a parameter for Dev/Test/Prod in the tool that would control which version of the code was run. Dev would run the code in the dev folder, test from test folder and prod from prod folder. Changes to the common dev script would not run when the tool was run from ArcCatalog. Closing ArcCatalog made no difference. Even though I selected Dev or Test it would always run from the prod folder.

    Adding reload(myCommonModule) to the tool script resolved this issue.

    0 讨论(0)
提交回复
热议问题