.pyc 文件何时刷新? [英] When are .pyc files refreshed?
问题描述
我知道.pyc"文件是纯文本.py"文件的编译版本,在运行时创建,以使程序运行得更快.但是我观察到了一些事情:
I understand that ".pyc" files are compiled versions of the plain-text ".py" files, created at runtime to make programs run faster. However I have observed a few things:
- 修改py"文件后,程序行为会发生变化.这表明py"文件已被编译,或者至少经过某种哈希处理或比较时间戳,以判断是否应重新编译它们.
- 删除所有.pyc"文件(
rm *.pyc
)后,有时程序行为会改变.这表明它们没有在.py"更新时被编译.
- Upon modification of "py" files, program behavior changes. This indicates that the "py" files are compiled or at least go though some sort of hashing process or compare time stamps in order to tell whether or not they should be re-compiled.
- Upon deleting all ".pyc" files (
rm *.pyc
) sometimes program behavior will change. Which would indicate that they are not being compiled on update of ".py"s.
问题:
- 他们如何决定何时编译?
- 有没有办法确保他们在开发过程中进行更严格的检查?
推荐答案
.pyc
文件仅在由其他脚本导入该 python 文件时创建(并可能覆盖).如果调用导入,Python 会检查 .pyc
文件的内部时间戳是否不早于相应的 .py
文件.如果是,则加载.pyc
;如果不存在或者 .pyc
尚不存在,Python 会将 .py
文件编译为 .pyc
并加载它.
The .pyc
files are created (and possibly overwritten) only when that python file is imported by some other script. If the import is called, Python checks to see if the .pyc
file's internal timestamp is not older than the corresponding .py
file. If it is, it loads the .pyc
; if it isn't or if the .pyc
does not yet exist, Python compiles the .py
file into a .pyc
and loads it.
更严格的检查"是什么意思?
What do you mean by "stricter checking"?
这篇关于.pyc 文件何时刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!