当前位置: 代码迷 >> python >> 如何在python3中编译python程序(转换为pyc)
  详细解决方案

如何在python3中编译python程序(转换为pyc)

热度:53   发布时间:2023-06-27 21:47:23.0

在python2中有两种生成pyc的方法:

1。

import py_compile
py_compile.compile("file.py")

2。

python -m compileall ./

python3怎么办? 我尝试了上述两种方法,都没有在python3中生成.pyc文件。 请帮忙。

谢谢@meissner_

  1. import py_compile

    py_compile.compile("file.py") #compiles single file named file.py

  2. python -m compileall ./ #combines all programs under current directory

两种方法都可以在Python2和Python3中使用。 Python2和Python3的唯一区别是:

  1. Python2同一目录中生成.pyc文件,而

  2. Python3在同一目录中名为__pycache__的文件夹中创建它。