我最初将所有模块都放在一个包中。我最近在原始包中创建了一个子包,并将一些模块移入其中。我的 src 文件夹和 2 个包文件夹都在我的 PYTHONPATH 中。
由于我重新定位了这些模块,我对它们的 .py 文件所做的更改似乎并没有进入生成的 *$py.class 文件,我相信这些文件最终会根据 print __FILE__
吐出。
.class 文件位于顶级包的目录中,我相信这是因为它们导入的第一个位置是在该包中的模块内。所以我认为他们来对地方了。
将模块移回顶级包确实可以解决问题,但被迫将所有模块放在一个包中几乎不是一种解决方案。除了将模块放在带有 __init__.py
的文件夹中之外,我还需要做些什么才能将模块“注册”为包的一部分吗?
注意:这个问题的其余部分只是使我得出结论:当我更改 .py 文件时 .class 文件没有更新的症状。如果您是一个 tl;dr 类型的人,您可能可以跳过它:P
我在函数的开头放置了一堆空格,当我单步执行它时,光标会跟随代码过去的位置是。
以下是 IDE 向我显示的代码:
这里是本地变量(请注意 self
没有任何绑定):
在我执行几行之后,这是代码(注意光标位置):
和当地人:
请注意,现在 self
已经有了 id< /code> 和 updatePeriod
绑定,因此空白后面的前两行代码显然已被执行。
如果我完全删除 .py 文件(将其粘贴在桌面或其他东西上),那么显然 IDE 找不到它,所以我无法单步执行它,但程序会根据以前的代码运行(有我可以看出一些明显的变化没有生效)。
最后,相关 *$py.class 文件的修改日期大约为 4.5 小时,尽管我在过去一两个小时内对最近的 .py 文件进行了所有这些摆弄。
I originally had all of my modules in one package. I recently created a sub-package in the original package, and moved a few modules into that. My src folder, and the 2 package folders, are all in my PYTHONPATH.
Since I relocated those modules, changes that I'm making to their .py files don't seem to be making their way into the generated *$py.class files, which I believe are ultimately what get run, based on what print __FILE__
spits out.
The .class files are located in the top-level package's directory, which I believe is because the first place they are imported from is within a module that's in that package. So I think they're in the right place.
Moving the modules back to the top-level package does make the problem go away, but being forced to have all modules in one package is hardly a solution. Is there something I have to do to 'register' a module as part of a package, other than having it in a folder with an __init__.py
?
Note: The rest of this question is just the symptoms that have caused me to conclude that the .class files are not being updated when I change the .py files. You can probably skip it if you're a tl;dr kind of person :P
I put a bunch of whitespace at the start of a function, and when I step through it, the cursor follows where the code used to be.
Here is the code the IDE shows me:
and here are the local vars (note that self
has nothing bound):
After I step a couple of lines, here is the code (note the cursor position):
and the locals:
Notice that now self
has had id
and updatePeriod
bound, so those first 2 lines of code after the whitespace have clearly been executed.
If I remove the .py file entirely (stick it on the desktop or something), then obviously the IDE can't find it, so I can't step through it, but the program runs based on what code used to be (there are some obvious changes that I can tell are not in effect).
Finally, the modification dates on the relevant *$py.class files are about 4.5 hours old, despite all this fiddling I've been doing with the recent .py files over the last hour or 2.
发布评论
评论(3)
当
.py
被删除时,已编译的 Python 文件不会自动删除。由于它们位于子包之前的$PYTHONPATH
中,因此它们会被执行,并且由于没有与它们对应的.py
,因此它们将被使用,并且永远不会被使用已更新。唯一的解决方案是手动删除它们。PyDev 通过实际解释源代码显然增加了混乱。
Compiled Python files don't get automatically removed, when
.py
gets removed. Since they are in$PYTHONPATH
before your sub-packages, they are executed, and since there is no.py
corresponding to them, they will get used and they will never be updated. The only solution is to manually remove them.PyDev apparently adds to confusion, by actually interpreting the source.
我认为这里有两个问题:
$py.class
:这可能是因为您将该文件作为主条目运行...如果我记得的话正确地,仅在导入文件时生成
$py.class
(即:不适用于您的__main__
模块)并且仅在代码实际更改时生成(我'我不确定 Jython 如何决定它被改变——可能是在文件,但我在这里可能是错的)。最好的解决方案是在移动文件及其相应的 $py.class(如移动文件夹)时删除 $py.class,这样您就可以 100% 确定 Jython 永远不会拾取它。
PyDev 可以帮助您:在
PyDev Package Explorer > 中选择一个文件夹PyDev >删除 *.pyc、*.pyo 和 *$py.class 文件
。$py.class
文件PyDev 实际上可以正确处理 .pyc 文件(即:当 .py 文件不再可用时删除 .pyc)。我正在更改 PyDev 来处理
$py.class
文件(所以,如果你在 3-4 小时内得到夜间,它应该可以工作 - 请参阅:http://pydev.org/download.html 了解获取它的说明 - 在此之前,您可以按照说明手动删除多于)。I think there are 2 issues here:
$py.class
is not regenerated:This is probably because you're running the file as the main entry... If I remember properly, the
$py.class
are only generated when the file gets imported (i.e.: not for your__main__
module) and only if the code has actually been changed (I'm not sure how Jython decides it was changed -- probably the time of the file, but I may be wrong here).The best solution for that would be deleting the $py.class when you move a file and its corresponding $py.class (like moving a folder), that way you can be 100% sure Jython will never pick it up.
PyDev can help you there: select a folder in the
PyDev Package Explorer > PyDev > Remove *.pyc, *.pyo and *$py.class Files
.$py.class
files without the corresponding .py filePyDev actually handles this properly for .pyc files (i.e.: deletes the .pyc when the .py file is no longer available). I'm changing PyDev to handle that for
$py.class
files too (so, if you get the nightly within 3-4 hours, it should be working -- see: http://pydev.org/download.html for instructions to get it -- until then, you can do the remove manually as described above).我这里也有同样的问题,我做了一些测试。
我知道这个话题有点老了,但我仍然想在这里添加一些内容。
$py.class
文件仅在导入.py class
时创建和更新某处。如果使用
execfile()
执行.py
文件,则$py.class
文件不会执行创建或更新。
I had the same question here, and I did a few tests.
I know this topic is kinda old, but still I would like to add something here.
The
$py.class
files only gets to be created and updated when the.py class
is being importedsomewhere. If a
.py
file is being executed withexecfile()
then the$py.class
file is notcreated or updated.