Linux 如何打包 python 脚本为elf可执行文件

1. 环境

  • Red Hat Enterprise Linux release 8.2 (Ootpa)
  • ppython36-3.6.8-2.module+el8.1.0+3334+5cb623d7.x86_64
  • pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)
  • pyinstaller 4.2

2 具体步骤

2.1 安装pyinstaller

1
pip3 install pyinstaller

2.2 执行打包 (cd 到 需要打包的 py 源代码的目录)

2.2.1 普通打包(非单文件模式)

  • 执行打包
    1
    # pyinstaller hiSam.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    62 INFO: PyInstaller: 4.2
    62 INFO: Python: 3.6.8
    63 INFO: Platform: Linux-4.18.0-193.el8.x86_64-x86_64-with-redhat-8.2-Ootpa
    64 INFO: wrote /tmp/hiSam.spec
    66 INFO: UPX is not available.
    69 INFO: Extending PYTHONPATH with paths
    ['/tmp', '/tmp']
    82 INFO: checking Analysis
    83 INFO: Building Analysis because Analysis-00.toc is non existent
    83 INFO: Initializing module dependency graph...
    85 INFO: Caching module graph hooks...
    93 WARNING: Several hooks defined for module 'win32ctypes.core'. Please take care they do not conflict.
    96 INFO: Analyzing base_library.zip ...
    5065 INFO: Caching module dependency graph...
    5194 INFO: running Analysis Analysis-00.toc
    5251 INFO: Analyzing /tmp/hiSam.py
    5254 INFO: Processing module hooks...
    5255 INFO: Loading module hook 'hook-difflib.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
    5258 INFO: Excluding import of doctest from module difflib
    5258 INFO: Loading module hook 'hook-encodings.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
    5350 INFO: Loading module hook 'hook-heapq.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
    5352 INFO: Excluding import of doctest from module heapq
    5352 INFO: Loading module hook 'hook-pickle.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
    5354 INFO: Excluding import of argparse from module pickle
    5354 INFO: Loading module hook 'hook-xml.py' from '/usr/local/lib/python3.6/site-packages/PyInstaller/hooks'...
    5768 INFO: Looking for ctypes DLLs
    5769 INFO: Analyzing run-time hooks ...
    5777 INFO: Looking for dynamic libraries
    6240 INFO: Looking for eggs
    6241 INFO: Using Python library /lib64/libpython3.6m.so.1.0
    6246 INFO: Warnings written to /tmp/build/hiSam/warn-hiSam.txt
    6279 INFO: Graph cross-reference written to /tmp/build/hiSam/xref-hiSam.html
    6292 INFO: checking PYZ
    6292 INFO: Building PYZ because PYZ-00.toc is non existent
    6292 INFO: Building PYZ (ZlibArchive) /tmp/build/hiSam/PYZ-00.pyz
    6833 INFO: Building PYZ (ZlibArchive) /tmp/build/hiSam/PYZ-00.pyz completed successfully.
    6837 INFO: checking PKG
    6837 INFO: Building PKG because PKG-00.toc is non existent
    6837 INFO: Building PKG (CArchive) PKG-00.pkg
    6861 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
    6863 INFO: Bootloader /usr/local/lib/python3.6/site-packages/PyInstaller/bootloader/Linux-64bit/run
    6863 INFO: checking EXE
    6863 INFO: Building EXE because EXE-00.toc is non existent
    6863 INFO: Building EXE from EXE-00.toc
    6864 INFO: Appending archive to ELF section in EXE /tmp/build/hiSam/hiSam
    6888 INFO: Building EXE from EXE-00.toc completed successfully.
    6890 INFO: checking COLLECT
    6890 INFO: Building COLLECT because COLLECT-00.toc is non existent
    6890 INFO: Building COLLECT COLLECT-00.toc
    6940 INFO: Building COLLECT COLLECT-00.toc completed successfully.
  • 打包结果检查
    1
    file dist/hiSam/hiSam
    1
    dist/hiSam/hiSam: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=1da3a1d77c7109ce6444919f4a15e7e6c63d02fa, stripped
  • 注意,非单文件打包是需要依赖它的库的,如果放到其他路径就会执行失败S
    1
    [37425] Error loading Python lib '/tmp/lee/libpython3.6m.so.1.0': dlopen: /tmp/lee/libpython3.6m.so.1.0: cannot open shared object file: No such file or directory

    2.2.2 单文件打包

  • 执行打包
    1
    # pyinstaller -F hiSam.py
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    60 INFO: PyInstaller: 4.2
    61 INFO: Python: 3.6.8
    63 INFO: Platform: Linux-4.18.0-193.el8.x86_64-x86_64-with-redhat-8.2-Ootpa
    64 INFO: wrote /tmp/hiSam.spec
    66 INFO: UPX is not available.
    68 INFO: Extending PYTHONPATH with paths
    ['/tmp', '/tmp']
    83 INFO: checking Analysis
    89 INFO: checking PYZ
    93 INFO: checking PKG
    94 INFO: Bootloader /usr/local/lib/python3.6/site-packages/PyInstaller/bootloader/Linux-64bit/run
    94 INFO: checking EXE
    95 INFO: Rebuilding EXE-00.toc because hiSam missing
    95 INFO: Building EXE from EXE-00.toc
    96 INFO: Appending archive to ELF section in EXE /tmp/dist/hiSam
    114 INFO: Building EXE from EXE-00.toc completed successfully.
  • 打包结果检查
    1
    # file dist/hiSam
    1
    dist/hiSam: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=1da3a1d77c7109ce6444919f4a15e7e6c63d02fa, stripped

    3 执行打包后的文件

    1
    2
    # ./hiSam
    Hi Sam , good to see you at AckDo.com