python 为啥自动生成pyc 文件??

图片描述

我平时运行的都是直白式的运行,为啥会出现pyc文件呢?

阅读 3.8k
3 个回答

写好a.py之后直接执行python3 a.py并不会生成a.pyc,只有在b.py中import a,然后运行b.py才会生成a.pyc
如果源文件没有更改的话,重新加载b模块,a.pyc也不会重新生成
pyc文件本身并不能加快运行时间,但是生成.pyc文件的过程会消耗时间,所以如果pyc文件已经被加载过了,程序的时间就会减少

摘自官方文档
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.

最后附上官方文档链接地址
https://docs.python.org/2/tut...

.pyc 是被使用过的py文件自动编译后的,发布软件时您可以只发布 .pyc 而不发布 .py 文件。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题