Python 强制执行字节码编译的做法? (.pyc)
我正在阅读有关 Python 解释器的内容,因为我无法理解为什么有些东西具有 python 编译对象(.pyc),而其他东西却没有。
我得到了问题的答案,但现在我很困惑。好吧,解释器将脚本编译为模块...如果我理解正确的话,它“有点”像 C 中的对象(这里是 C 程序员,Python 新手) - 或者我猜更像是 .class在Java中,因为它是编译的字节码,而不是本机指令......无论如何,当您导入脚本或明确调用它进行编译时(由于某种原因不太有利),它都会执行此操作。
那么根据这种理解,编译后的字节码与未编译的字节码之间是否存在运行时差异?假设只有一个解释器(字节码解释器),这意味着如果模块尚未编译,它必须在进行解释之前执行语法/词法分析/解析(编译)。这不会导致执行时间更长吗?
因此,如果您认为上述情况是正确的,那么显然最好将模块编译为 .pyc,而不是作为标准 .py 脚本即时运行。
这是否意味着最好在主运行中执行尽可能少的执行?
我想,如果你的入口点有任何硬核逻辑(即我的入口点有几次树遍历和其他繁重的比较),那么入口点本身不应该被包装以便编译吗?
也就是说,而不是:这样
# file.py:
def main():
<stuff goes here - setup, whatever shared resources different modules need, etc.>
main()
做会更好吗:
# wrapper.py:
from file.py import *
main()
希望我解释了我要问的内容,足够好。我很可能对 Python 中如何使用解释器/编译器有错误的理解,而且这个问题根本不合理——我对 Python 还很陌生。
TIA
I was reading up about the Python interpreter because I couldn't understand why some things had the python compiled objects (.pyc), but others didn't.
I got the answer to my question, but now I'm confused. So okay, the interpreter compiles a script to a module...which is 'sort of' like an object in C if I'm understanding this correctly (C programmer here, new to Python) - or I guess more like a .class in Java since it's compiled bytecode, not native instructions...anyway it does this when either you import a script, OR if you explicitly call it to be compiled (which is for some reason less favorable).
So under that understanding, is there any runtime difference between compiled bytecode and not? Assuming there's only one interpreter (a bytecode interpreter), it would mean that if the module isn't already compiled, it has to do the grammar/lexing/parsing (compiling) right before it does the interpretation. Won't that lead to a higher execution time?
So if you take the above to be true, then it's obviously best if the modules are compiled into .pyc, not ran as a standard .py script on the fly.
Would that mean it's best to have as minimal execution in your main run as possible?
I would think, if your entry point has any hardcore logic (ie. mine has a couple tree traversals, and other heavy compares), shouldn't that entry point in and of itself be wrapped so it's compiled?
That is, instead of:
# file.py:
def main():
<stuff goes here - setup, whatever shared resources different modules need, etc.>
main()
Would it be better to do:
# wrapper.py:
from file.py import *
main()
Hope I explained what I'm asking, well enough. It's quite possible that I got the wrong understanding of how the interpreter/compiler is used in Python and this question isn't even reasonable to ask - I'm quite new to Python.
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是对的:
1) .pyc 文件是 Python 源代码编译为字节码的缓存副本
2) 导入模块时创建 .pyc 文件
3) 您在 python 命令行中命名的主程序不是编译为.pyc
4) 因此主程序每次运行时都会被编译。
但是:
主程序被编译为字节码,然后解释字节码,因此只有很小的开销(编译),主程序中的所有执行都没有持续的开销。编译的额外时间与文件中的行数成正比,而不是与执行这些行的次数成正比。
TL;博士:别担心。
You are correct:
1) A .pyc file is a cached copy of the compilation of Python source to bytecode
2) A .pyc file is created when a module is imported
3) The main program that you name on the python command line is not compiled to a .pyc
4) Therefore the main program is compiled every time it is run.
But:
The main program is compiled to bytecode, and then the bytecode is interpreted, so there's only a small overhead (the compilation), there isn't an ongoing overhead to all execution in the main program. The extra time for compiling is proportional to the number of lines in the file, not the number of time those lines are executed.
tl;dr: don't worry about it.
这是针对初学者的,
Python 在运行脚本之前会自动将脚本编译为已编译代码,即所谓的字节代码。
运行脚本不被视为导入,并且不会创建 .pyc。
例如,如果您有一个脚本文件 abc.py 导入了另一个模块 xyz.py,则当您运行 abc.py 时,由于导入了 xyz,因此将创建 xyz.pyc,但自 abc 以来将不会创建 abc.pyc 文件。 py 没有被导入。
如果需要为未导入的模块创建 .pyc 文件,可以使用 py_compile 和compileall 模块。
py_compile 模块可以手动编译任何模块。一种方法是以交互方式使用该模块中的 py_compile.compile 函数:
这会将 .pyc 写入与 abc.py 相同的位置(您可以使用可选参数 cfile 覆盖它)。
有关更多信息: http://effbot.org/pyfaq /how-do-i-create-a-pyc-file.htm
This is for beginners,
Python automatically compiles your script to compiled code, so called byte code, before running it.
Running a script is not considered an import and no .pyc will be created.
For example, if you have a script file abc.py that imports another module xyz.py, when you run abc.py, xyz.pyc will be created since xyz is imported, but no abc.pyc file will be created since abc.py isn’t being imported.
If you need to create a .pyc file for a module that is not imported, you can use the py_compile and compileall modules.
The py_compile module can manually compile any module. One way is to use the py_compile.compile function in that module interactively:
This will write the .pyc to the same location as abc.py (you can override that with the optional parameter cfile).
For more: http://effbot.org/pyfaq/how-do-i-create-a-pyc-file.htm