• 欢迎访问DBA的辛酸事儿,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站
  • 欢迎大家关注博主公众号:DBA的辛酸事儿
  • 博文中若有错误的地方,请大家指正,大家的指正是我前进的动力

如何将Python程序打包成linux可执行文件?

Linux SEian.G 2年前 (2022-04-22) 2410次浏览 已收录 0个评论
文章目录[隐藏]

在日常的数据库运维管理中,经常要写一大堆的Python脚本,那么如何将Python脚本打包成可执行的程序,本文就针对该需求将Python脚本打包成Linux可执行文件,具体步骤如下:

环境说明

OS:CentOS 7

Python:2.7

1、安装pyinstaller

使用的工具是pyinstaller,打开终端输入sudo pip install pyinstaller

# pip install pyinstaller
Collecting pyinstaller
  Downloading https://pypi.srvxxx/packages/cf/b0/69585629b023056e801a99ee4d669e2d3b85fb5a68fe461c1660af9ea514/pyinstaller-5.0.tar.gz (2.8MB)
    100% |████████████████████████████████| 2.8MB 455kB/s 
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "", line 1, in 
      File "/tmp/pip-build-oLQxj4/pyinstaller/setup.py", line 73
        file=sys.stderr
            ^
    SyntaxError: invalid syntax
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-oLQxj4/pyinstaller/

python2.7直接安装pyinstaller会报错,版本5与python2不兼容,所以我们安装时需指定兼容的pyinstaller版本号。安装命令如下:

# pip2 install pyinstaller==3.2.1
Collecting pyinstaller==3.2.1
  Downloading https://pypi.srv.xxx/packages/3f/d2/3515242cc5cfed12706506d17728a7ee0b8cf33840e250357fd793a94607/PyInstaller-3.2.1.tar.bz2 (2.4MB)
    100% |████████████████████████████████| 2.4MB 500kB/s 
Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/lib/python2.7/site-packages (from pyinstaller==3.2.1)
Installing collected packages: pyinstaller
  Running setup.py install for pyinstaller ... done
Successfully installed pyinstaller-3.2.1

安装完成后,输入pyinstaller测试是否成功。

# pyinstaller
usage: pyinstaller [-h] [-v] [-D] [-F] [--specpath DIR] [-n NAME]
                   [--add-data <SRC;DEST or SRC:DEST>]
                   [--add-binary <SRC;DEST or SRC:DEST>] [-p DIR]
                   [--hidden-import MODULENAME]
                   [--additional-hooks-dir HOOKSPATH]
                   [--runtime-hook RUNTIME_HOOKS] [--exclude-module EXCLUDES]
                   [--key KEY] [-d] [-s] [--noupx] [-c] [-w]
                   [-i ]
                   [--version-file FILE] [-m ] [-r RESOURCE]
                   [--uac-admin] [--uac-uiaccess] [--win-private-assemblies]
                   [--win-no-prefer-redirects]
                   [--osx-bundle-identifier BUNDLE_IDENTIFIER]
                   [--distpath DIR] [--workpath WORKPATH] [-y]
                   [--upx-dir UPX_DIR] [-a] [--clean] [--log-level LEVEL]
                   [--upx UPX]
                   scriptname [scriptname ...]
pyinstaller: error: too few arguments

2、打包程序

首先在打包之前检查一下py文件是否可以正常执行,确认无误后进行打包

# pyinstaller -F test.py 
35 INFO: PyInstaller: 3.2.1
36 INFO: <a href="http://www.seiang.com/?tag=python" title="查看更多关于Python的文章" target="_blank">Python</a>: 2.7.5
36 INFO: Platform: Linux-3.10.0-1127.19.1.el7.x86_64-x86_64-with-centos-7.8.2003-Core
36 INFO: wrote /root/test/test.spec
51 INFO: UPX is not available.
52 INFO: Extending PYTHONPATH with paths
['/root/test', '/root/test']
53 INFO: checking Analysis
53 INFO: Building Analysis because out00-Analysis.toc is non existent
53 INFO: Initializing module dependency graph...
55 INFO: Initializing module graph hooks...
99 INFO: running Analysis out00-Analysis.toc
117 INFO: Caching module hooks...
119 INFO: Analyzing /root/test/test.py
1315 INFO: Processing pre-safe import module hook   six.moves
1744 INFO: Processing pre-safe import module hook   _xmlplus
2604 INFO: Loading module hooks...
2604 INFO: Loading module hook "hook-xml.py"...
2643 INFO: Loading module hook "hook-httplib.py"...
2643 INFO: Loading module hook "hook-requests.py"...
2645 INFO: Loading module hook "hook-encodings.py"...
3049 INFO: Looking for ctypes DLLs
3202 INFO: Analyzing run-time hooks ...
3211 INFO: Looking for dynamic libraries
3627 INFO: Looking for eggs
3627 INFO: Using <a href="http://www.seiang.com/?tag=python" title="查看更多关于Python的文章" target="_blank">Python</a> library /lib64/libpython2.7.so.1.0
3631 INFO: Warnings written to /root/test/build/test/warntest.txt
3688 INFO: checking PYZ
3688 INFO: Building PYZ because out00-PYZ.toc is non existent
3688 INFO: Building PYZ (ZlibArchive) /root/test/build/test/out00-PYZ.pyz
4020 INFO: Building PYZ (ZlibArchive) /root/test/build/test/out00-PYZ.pyz completed successfully.
4067 INFO: checking PKG
4067 INFO: Building PKG because out00-PKG.toc is non existent
4067 INFO: Building PKG (CArchive) out00-PKG.pkg
6421 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
6430 INFO: Bootloader /usr/lib/python2.7/site-packages/PyInstaller/bootloader/Linux-64bit/run
6430 INFO: checking EXE
6430 INFO: Building EXE because out00-EXE.toc is non existent
6430 INFO: Building EXE from out00-EXE.toc
6431 INFO: Appending archive to ELF section in EXE /root/test/dist/test
6462 INFO: Building EXE from out00-EXE.toc completed successfully.

打包成功后,目录如下:

# ll
total 8
drwxr-xr-x 3 root root   18 Apr 22 16:00 build
drwxr-xr-x 2 root root   18 Apr 22 16:00 dist
-rw-r--r-- 1 root root 3130 Apr 22 15:51 test.py
-rw-r--r-- 1 root root  673 Apr 22 16:00 test.spec

会在dist目录下生成一个可执行的文件

# ll
total 4764
-rwxr-xr-x 1 root root 4876696 Apr 22 16:31 test

# ./test --list -u xh -g xx
Please enter your passwd:1234
测试成功了

到此,一个python脚本就打包成了一个可执行的文件了,无论将可执行文件放到任何路径下都可以执行了

如何将Python程序打包成linux可执行文件?


如果您觉得本站对你有帮助,那么可以收藏和推荐本站,帮助本站更好地发展,在此谢过各位网友的支持。
转载请注明原文链接:如何将Python程序打包成linux可执行文件?
喜欢 (1)
SEian.G
关于作者:
用心去记录工作,用心去感受生活,用心去学着成长;座右铭:苦练七十二变,笑对八十一难
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址