如何从 .py 文件手动生成 .pyc 文件 [英] How can I manually generate a .pyc file from a .py file

查看:51
本文介绍了如何从 .py 文件手动生成 .pyc 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我无法依赖 Python 的import"语句自动生成 .pyc 文件

For some reason, I can not depend on Python's "import" statement to generate .pyc file automatically

有没有办法实现如下功能?

Is there a way to implement a function as following?

def py_to_pyc(py_filepath, pyc_filepath):
    ...

推荐答案

您可以在终端中使用 compileall.以下命令将递归进入子目录,并为它找到的所有 python 文件制作 pyc 文件.compileall 模块是 python 标准库的一部分,所以你不需要安装任何额外的东西来使用它.这对 python2 和 python3 的工作方式完全相同.

You can use compileall in the terminal. The following command will go recursively into sub directories and make pyc files for all the python files it finds. The compileall module is part of the python standard library, so you don't need to install anything extra to use it. This works exactly the same way for python2 and python3.

python -m compileall .

这篇关于如何从 .py 文件手动生成 .pyc 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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