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

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

问题描述

为什么要编译一个Python脚本?你可以直接从.py文件运行它,它工作正常,所以有性能优势或东西吗?



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



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



Ben Blank




值得注意的是,运行
脚本有更快的启动

时间(因为它不需要编译
),它不会运行任何
更快。



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?

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.

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.

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天全站免登陆