为什么要编译 Python 代码? [英] Why compile Python code?

查看:26
本文介绍了为什么要编译 Python 代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么要编译 Python 脚本?您可以直接从 .py 文件中运行它们并且它工作正常,那么是否有性能优势或其他什么?

Why would you compile a Python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something?

我还注意到我的应用程序中的某些文件被编译为 .pyc 而其他文件则没有,这是为什么?

I also notice that some files in my application get compiled into .pyc while others do not, why is this?

推荐答案

它被编译成字节码,可以更快地使用.

It's compiled to bytecode which can be used much, much, much faster.

某些文件未编译的原因是每次运行脚本时都会重新编译您使用 python main.py 调用的主脚本.所有导入的脚本都将被编译并存储在磁盘上.

The reason some files aren't compiled is that the main script, which you invoke with python main.py is recompiled every time you run the script. All imported scripts will be compiled and stored on the disk.

Ben Blank 的重要补充:

Important addition by Ben Blank:

值得注意的是,在运行一个编译后的脚本具有更快的启动时间(因为它不需要编译),它不会运行任何更快.

It's worth noting that while running a compiled script has a faster startup time (as it doesn't need to be compiled), it doesn't run any faster.

这篇关于为什么要编译 Python 代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆