Python .pyc 文件何时刷新?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15839555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When are .pyc files refreshed?
提问by Aaron Schif
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:
我知道“.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.
- 修改“py”文件后,程序行为会发生变化。这表明“py”文件被编译或至少通过某种散列过程或比较时间戳来判断它们是否应该重新编译。
- 删除所有“.pyc”文件 (
rm *.pyc) 后,有时程序行为会改变。这表明它们没有在“.py”更新时被编译。
Questions:
问题:
- How do they decide when to be compiled?
- Is there a way to ensure that they have stricter checking during development?
- 他们如何决定何时编译?
- 有没有办法确保他们在开发过程中进行更严格的检查?
采纳答案by DaveTheScientist
The .pycfiles 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 .pycfile's internal timestamp is not older than the corresponding .pyfile. If it is, it loads the .pyc; if it isn't or if the .pycdoes not yet exist, Python compiles the .pyfile into a .pycand loads it.
该.pyc文件被创建(并可能覆盖)仅当蟒蛇文件是否已被其他脚本导入。如果调用导入,Python 会检查.pyc文件的内部时间戳是否不早于相应.py文件。如果是,则加载.pyc; 如果.pyc不存在或不存在,Python会将.py文件编译为 a.pyc并加载它。
What do you mean by "stricter checking"?
“更严格的检查”是什么意思?
回答by Zags
.pyc files generated whenever the corresponding code elements are imported, and updated if the corresponding code files have been updated. If the .pyc files are deleted, they will be automatically regenerated. However, they are notautomatically deleted when the corresponding code files are deleted.
每当导入相应的代码元素时都会生成 .pyc 文件,如果相应的代码文件已更新,则更新。如果 .pyc 文件被删除,它们将自动重新生成。但是,删除相应的代码文件时不会自动删除它们。
This can cause some really fun bugs during file-level refactors.
这可能会在文件级重构期间导致一些非常有趣的错误。
First of all, you can end up pushing code that only works on your machine and on no one else's. If you have dangling references to files you deleted, these will still work locally if you don't manually delete the relevant .pyc files because .pyc files can be used in imports. This is compounded with the fact that a properly configured version control system will only push .py files to the central repository, not .pyc files, meaning that your code can pass the "import test" (does everything import okay) just fine and not work on anyone else's computer.
首先,您最终可能会推送仅适用于您的机器而不适用于其他人的代码。如果您对删除的文件有悬空引用,如果您不手动删除相关的 .pyc 文件,这些文件仍将在本地工作,因为 .pyc 文件可用于导入。再加上正确配置的版本控制系统只会将 .py 文件推送到中央存储库,而不是 .pyc 文件,这意味着您的代码可以通过“导入测试”(一切都可以导入)就好了,而不是在别人的电脑上工作。
Second, you can have some pretty terrible bugs if you turn packages into modules. When you convert a package (a folder with an __init__.pyfile) into a module (a .py file), the .pyc files that once represented that package remain. In particular, the __init__.pycremains. So, if you have the package foo with some code that doesn't matter, then later delete that package and create a file foo.py with some function def bar(): passand run:
其次,如果您将包转换为模块,您可能会遇到一些非常可怕的错误。当您将包(包含文件的__init__.py文件夹)转换为模块(.py 文件)时,曾经代表该包的 .pyc 文件仍然存在。尤其是__init__.pyc遗迹。所以,如果你的包 foo 有一些无关紧要的代码,那么稍后删除该包并创建一个带有一些函数的文件 foo.pydef bar(): pass并运行:
from foo import bar
you get:
你得到:
ImportError: cannot import name bar
because python is still using the old .pyc files from the foo package, none of which define bar. This can be especially problematic on a web server, where totally functioning code can break because of .pyc files.
因为 python 仍在使用 foo 包中的旧 .pyc 文件,其中没有一个定义 bar。这在 Web 服务器上尤其成问题,因为 .pyc 文件,完全正常运行的代码可能会中断。
As a result of both of these reasons (and possibly others), your deployment code and testing code should delete .pyc files, such as with the following line of bash:
由于这两个原因(也可能是其他原因),您的部署代码和测试代码应该删除 .pyc 文件,例如使用以下 bash 行:
find . -name '*.pyc' -delete
Also, as of python 2.6, you can run python with the -Bflag to not use .pyc files. See How to avoid .pyc files?for more details.
此外,从 python 2.6 开始,您可以运行带有-B不使用 .pyc 文件的标志的python 。请参阅如何避免 .pyc 文件?更多细节。
See also: How do I remove all .pyc files from a project?
另请参阅:如何从项目中删除所有 .pyc 文件?

