Python .py 和 .pyc 文件有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3918689/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:20:14  来源:igfitidea点击:

What is the difference between .py and .pyc files?

pythonfile

提问by

I have noticed .pycfiles spontaneously being generated when some .pyfile of the same name gets run. What is the difference between .pyand .pycfiles?

我注意到.pyc当运行某些.py同名文件时会自发生成文件。.py.pyc文件和有什么不一样?

Also, I find that having .pycfiles lying around clutters up space. Should one delete .pycfiles? Or is there a benefit and/or necessity to having them around?

此外,我发现将.pyc文件放置在周围会使空间变得混乱。应该删除.pyc文件吗?或者让他们在身边是否有好处和/或必要性?

UPDATE: Here are 2 answered questions that are related to my question

更新:这里有 2 个与我的问题相关的已回答问题

If Python is interpreted, what are .pyc files?

如果解释 Python,什么是 .pyc 文件?

Why are main runnable Python scripts not compiled to pyc files like modules?

为什么主要的可运行 Python 脚本没有像模块一样编译为 pyc 文件?

This Question is not a Duplicate

这个问题不是重复的

Reason 1: Because I am asking what the difference between these two files are. The question S.Lott found named 'If Python is interpreted, what are .pyc files?' is not asking what the difference between .py and .pyc files are. It is asking what .pyc files are.

原因1:因为我在问这两个文件有什么区别。S.Lott 发现的问题名为“如果解释 Python,什么是 .pyc 文件?” 不是问 .py 和 .pyc 文件之间的区别是什么。它在询问 .pyc 文件是什么。

Reason 2: Because my secondary questions 'Should one delete .pycfiles? Or is there a benefit and/or necessity to having them around?' provide even more information on .pyc files and how one should handle them.

原因 2:因为我的次要问题是“应该删除.pyc文件吗?或者让他们在身边是否有好处和/或必要性?提供关于 .pyc 文件的更多信息以及如何处理它们。

Reason 3: Because when a beginner Python programmer like myself wants to find out What is the difference between .py and .pyc files?, they will have no problem finding out the answer as they will be guided directly to my question. This helps reduce search time since the question is right to the point.

原因三:因为当像我这样的 Python 初学者想找出.py 和 .pyc 文件的区别是什么?,他们会很容易找到答案,因为他们会被直接引导到我的问题。这有助于减少搜索时间,因为问题切中要害。

回答by meder omuraliev

Python compiles the .pyand saves files as .pycso it can reference them in subsequent invocations.

Python 编译.py并保存文件,.pyc以便在后续调用中引用它们。

There's no harm in deleting them, but they will save compilation time if you're doing lots of processing.

删除它们没有坏处,但如果您进行大量处理,它们将节省编译时间。

回答by mipadi

.pyccontain the compiled bytecode of Python source files. The Python interpreter loads .pycfiles before .pyfiles, so if they're present, it can save some time by not having to re-compile the Python source code. You can get rid of them if you want, but they don't cause problems, they're not big, and they may save some time when running programs.

.pyc包含 Python 源文件的编译字节码。Python 解释器在.pyc文件之前加载文件.py,因此如果它们存在,它可以通过不必重新编译 Python 源代码来节省一些时间。如果你愿意,你可以摆脱它们,但它们不会引起问题,它们并不大,并且它们在运行程序时可能会节省一些时间。

回答by Name

"A program doesn't run any faster when it is read from a ".pyc" or ".pyo" file than when it is read from a ".py" file; the only thing that's faster about ".pyc" or ".pyo" files is the speed with which they are loaded. "

“从“.pyc”或“.pyo”文件中读取程序时,它的运行速度并不比从“.py”文件中读取时快;只有“.pyc”或“ .pyo”文件是加载它们的速度。”

http://docs.python.org/release/1.5.1p1/tut/node43.html

http://docs.python.org/release/1.5.1p1/tut/node43.html