Python缓存的字节码(pyc)文件何时更新?

2024-05-20 02:04:14 发布

您现在位置:Python中文网/ 问答频道 /正文

有时我通过指向make PYTHON_TEST=path_of_module_to_test test在特定的模式上运行unittest,如果这个模块path_of_module_to_test test导入了其他一些更新的python模块,那么从这个模块导入的内容是从更新的py源文件还是从未更新的pyc文件中获取的,还是导入会导致相关pyc文件的更新?在


Tags: 模块文件oftopathpytest内容
1条回答
网友
1楼 · 发布于 2024-05-20 02:04:14

来自PEP 3147

CPython compiles its source code into "byte code", and for performance reasons, it caches this byte code on the file system whenever the source file has changes. This makes loading of Python modules much faster because the compilation phase can be bypassed. When your source file is foo.py , CPython caches the byte code in a foo.pyc file right next to the source.

如果源代码发生变化,CPython将重新编译并重新缓存字节码。在

请注意,以上是针对Python 2.x。{cd2}中的{

注意:当我们在这里提到“CPython”时,我们指的是您最可能从https://www.python.org使用的Python实现,因为这种行为(我相信)是特定于实现的。在

相关问题 更多 >