【问题标题】:May I omit .pyo and .pyc files in an RPM?我可以省略 RPM 中的 .pyo 和 .pyc 文件吗?
【发布时间】:2013-08-21 03:35:48
【问题描述】:

我将 Python 应用程序及其 virtenv 环境捆绑在 RPM 中,以便于部署。省略所有 .pyo.pyc 文件是否明智?

我要做的是在 virtenv 实例内的安装后操作中调用 compileall.py。这行得通吗,还是会搞砸?

注意:我意识到我可以在一台机器上尝试,但是 a.)这不会给我一个关于这是否可以在其他机器上工作的结论性答案,并且 b.)其他人可能有同样的问题,而我没有'找不到答案。

【问题讨论】:

    标签: python python-2.7 virtualenv rpm


    【解决方案1】:

    您确实可以省略它们 - 它们要么是生成的(如果您有写入权限),要么是在您每次导入时解析 .py(这会花费时间)。

    但是,根据您的发行版,您的 RPM 系统可能包含用于编译 .py 文件的简单脚本,并在发行版上捆绑 .pyo.pyc 文件,这使任务变得非常容易。

    $ rpm --showrc | grep -A 7 py.*_compile
    -14: py3_compile(O)
    find %1 -name '*.pyc' -exec rm -f {} ";"
    python3 -c "import sys, os, compileall; br='%{buildroot}'; compileall.compile_dir(sys.argv[1], ddir=br and (sys.argv[1][len(os.path.abspath(br)):]+'/') or None)" %1
    %{-O:
    find %1 -name '*.pyo' -exec rm -f {} ";"
    python3 -O -c "import sys, os, compileall; br='%{buildroot}'; compileall.compile_dir(sys.argv[1], ddir=br and (sys.argv[1][len(os.path.abspath(br)):]+'/') or None)" %1
    }
    -14: py3_incdir /usr/include/python3.3m
    --
    -14: py_compile(O)
    find %1 -name '*.pyc' -exec rm -f {} \;
    python -c "import sys, os, compileall; br='%{buildroot}'; compileall.compile_dir(sys.argv[1], ddir=br and (sys.argv[1][len(os.path.abspath(br)):]+'/') or None)" %1
    %{-O:
    find %1 -name '*.pyo' -exec rm -f {} \;
    python -O -c "import sys, os, compileall; br='%{buildroot}'; compileall.compile_dir(sys.argv[1], ddir=br and (sys.argv[1][len(os.path.abspath(br)):]+'/') or None)" %1
    }
    -14: py_incdir  %{py_prefix}/include/python%{py_ver}
    

    我。例如,您可以分别输入%py_compile%py3_compile 进入你的%build 部分,你就有了你需要的东西。

    但是,如上所述,如果您想在多个不同版本号的 Python 安装中使用它们,也可以省略它们。但是,您应该确保永远不会创建 .pyc.pyo 文件,因为这可能会搞砸。

    【讨论】:

    • 哇,不错。不过,有没有办法覆盖使用的python?正如我在问题中所写,它位于virtenv 中。旁注:省略它们的主要原因是生成的包大小。
    • 您可以通过替换 .rpmrc.rpmmacros 文件中提到的宏来覆盖它。
    • 使用--define 开关本身,因为整个构建步骤是脚本化的,并且一些定义正在计算中。
    【解决方案2】:

    只要您有他们的.py 文件,它就是安全的。因为.pyo.pyc 文件是从原始.py 文件生成的。

    另请参阅What do the python file extensions, .pyc .pyd .pyo stand for?If Python is interpreted, what are .pyc files?

    【讨论】:

    • 谢谢。我知道它们是什么,但我仍然想在 RPM 安装后重新生成它们。所以我的问题有点超出了你的回答范围:)(也w.r.t.我使用virtenv这似乎改变了一些规则)
    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 2013-07-02
    • 2019-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多