python 刷新/重新加载

发布于 2024-08-06 20:36:17 字数 308 浏览 3 评论 0原文

这是一个非常基本的问题 - 但我无法通过在线搜索找到答案。

我正在使用 python 来控制 ArcGIS,并且我有一个简单的 python 脚本,它调用一些预先编写的代码。

但是,当我对预先编写的代码进行更改时,它似乎不会导致任何更改。我导入这个模块,并尝试刷新它,但没有任何反应。

我什至将它调用的文件移动到另一个位置,并且脚本仍然可以正常工作。我昨天做的一件事是将所有 python 文件所在的文件夹添加到 sys 路径(使用 sys.append('path') ),我想知道这是否会产生影响。

预先感谢,并对草率的术语表示歉意。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

终止放荡 2024-08-13 20:36:17

目前尚不清楚“刷新”的含义,但 Python 的正常行为是您需要重新启动软件才能以新的方式查看 Python 模块并重新读取它。

如果您的更改即使在重新启动后也没有得到处理,那么这是由于以下两个错误之一造成的:

  1. pyc 文件上的时间戳不正确并且是未来某个时间。
  2. 您实际上正在编辑错误的文件。

即使不使用 reload() 命令重新启动软件,您也可以使用 reload 重新读取文件。请注意,指向模块中任何内容的任何变量都需要在重新加载后重新导入。像这样的东西:

import themodule
from themodule import AClass

reload(themodule)
from themodule import AClass

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
寂寞陪衬 2024-08-13 20:36:17

一种方法是调用 reload

示例:以下是 foo.py 的内容:

def bar():
    return 1

在交互式会话中,我可以执行以下操作:

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

然后在另一个窗口中,我可以将 foo.py 更改为:

def bar():
    return "Hello"

返回交互式会话,调用 foo.bar() 仍然返回 1,直到我这样做:

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

调用 reload 是确保模块启动的一种方法-即使磁盘上的文件已更改,也是最新的。它不一定是最有效的(您可能最好检查文件的上次修改时间或使用类似 pyinotify重新加载之前),但实施起来肯定很快。

Python 不会每次都从源模块读取的原因之一是加载模块(相对)昂贵 - 如果您有一个 300kb 的模块并且只使用文件中的单个常量怎么办? Python 加载模块一次并将其保留在内存中,直到您重新加载它。

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.

长途伴 2024-08-13 20:36:17

如果您在 IPython shell 中运行,则存在一些神奇的命令。

IPython 文档介绍了这个名为 autoreload 扩展 的功能。

最初,我从 Jonathan March 关于这个主题的博客文章(请参阅该链接中的第 3 点)。

基本上您所要做的就是以下操作,您所做的更改将在保存后自动反映出来:

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: Import MODULE

In [4]: my_class = Module.class()
        my_class.printham()
Out[4]: ham

In [5]: #make changes to printham and save
In [6]: my_class.printham() 
Out[6]: hamlet

If you are running in an IPython shell, then there are some magic commands that exist.

The IPython docs cover this feature called the autoreload extension.

Originally, I found this solution from Jonathan March's blog posting on this very subject (see point 3 from that link).

Basically all you have to do is the following, and changes you make are reflected automatically after you save:

In [1]: %load_ext autoreload

In [2]: %autoreload 2

In [3]: Import MODULE

In [4]: my_class = Module.class()
        my_class.printham()
Out[4]: ham

In [5]: #make changes to printham and save
In [6]: my_class.printham() 
Out[6]: hamlet
ζ澈沫 2024-08-13 20:36:17

我在从模块内导入所有对象时使用了以下内容,以确保 web2py 使用我当前的代码:

import buttons
import table
reload(buttons)
reload(table)
from buttons import *
from table import *

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 *
[浮城] 2024-08-13 20:36:17

我不太确定这就是你的意思,所以请随时纠正我。您正在程序中导入一个模块(我们称之为 mymodule.py),但是当您更改其内容时,您看不到差异?

Python 不会在每次使用时查找 mymodule.py 中的更改,它会第一次加载它,将其编译为字节码并在内部保存。它通常还会保存编译后的字节码(mymodule.pyc)。下次启动程序时,它将检查 mymodule.py 是否比 mymodule.pyc 更新,并在必要时重新编译它。

如果需要,您可以显式重新加载模块:

import mymodule

[... some code ...]

if userAskedForRefresh:
    reload(mymodule)

当然,它比这更复杂,并且您可能会产生副作用,具体取决于您对程序对其他模块所做的操作,例如,如果变量取决于中定义的类我的模块。

或者,您可以使用 execfile 函数(或 exec()eval()compile()

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())

屋顶上的小猫咪 2024-08-13 20:36:17

我在为 ArcGIS 10.2 创建地理处理脚本时遇到了完全相同的问题。我有一个 python 工具箱脚本、一个工具脚本和一个通用脚本。我在工具中有一个用于开发/测试/生产的参数,可以控制运行哪个版本的代码。 Dev 将运行 dev 文件夹中的代码,从 test 文件夹中运行测试,从 prod 文件夹中运行 prod。当从 ArcCatalog 运行该工具时,对通用开发脚本的更改将不会运行。关闭 ArcCatalog 没有任何影响。即使我选择了“开发”或“测试”,它始终会从 prod 文件夹运行。

将 reload(myCommonModule) 添加到工具脚本解决了此问题。

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.

梦巷 2024-08-13 20:36:17

对于不同版本的Python,情况会有所不同。
以下显示了 python 3.4 版本或更高版本的示例:

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?

对于较早的 python 版本(例如 2.x),请使用如上所述的内置重新加载功能。
更好的是使用 ipython3,因为它提供自动重新加载功能。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文