python的程序的缺点是: 需要提供源码,如果不想让源码被第三方获得,可以将其打包成二进制文件的形式。
pyinstaller就可以完成这个要求。
安装pyinstaller
wget -c https://github.com/pyinstaller/pyinstaller/releases/download/v3.3.1/PyInstaller-3.3.1.tar.gz
tar -zxvf PyInstaller-3.3.1.tar.gz
python setup.py install
pyinstaller --version
编写测试python程序
vim test.py
#!/usr/bin/python -w
# -*- coding:utf8 -*-
import os
def main():
print("print the result of 'ls -al':")
os.system("ls -al")
if __name__ == '__main__':
main()
直接用python程序调用,结果如下:
[root@localhost proj]# python test.py
print the result of 'ls -al':
total 5
drwxrwxrwx 1 root root 4096 Jul 6 18:33 .
drwxr-xr-x. 4 root root 33 Jun 11 08:39 ..
-rwxrwxrwx 1 root root 174 Jul 6 18:22 test.py
pyinstaller打包
pyinstaller -F test.py
大写的-F表示生成单一文件。
运行结果:
[root@localhost proj]# pyinstaller -F test.py
54 INFO: PyInstaller: 3.3.1
54 INFO: Python: 2.7.5
54 INFO: Platform: Linux-3.10.0-514.26.2.el7.x86_64-x86_64-with-centos-7.3.1611-Core
57 INFO: wrote /mnt/proj/test.spec
62 INFO: UPX is not available.
67 INFO: Extending PYTHONPATH with paths
['/mnt/proj', '/mnt/proj']
67 INFO: checking Analysis
68 INFO: Building Analysis because out00-Analysis.toc is non existent
68 INFO: Initializing module dependency graph...
71 INFO: Initializing module graph hooks...
121 INFO: running Analysis out00-Analysis.toc
140 INFO: Caching module hooks...
142 INFO: Analyzing /mnt/proj/test.py
1349 INFO: Loading module hooks...
1349 INFO: Loading module hook "hook-encodings.py"...
1825 INFO: Looking for ctypes DLLs
1825 INFO: Analyzing run-time hooks ...
1828 INFO: Looking for dynamic libraries
2263 INFO: Looking for eggs
2263 INFO: Using Python library /lib64/libpython2.7.so.1.0
2266 INFO: Warnings written to /mnt/proj/build/test/warntest.txt
2286 INFO: Graph cross-reference written to /mnt/proj/build/test/xref-test.html
2375 INFO: checking PYZ
2376 INFO: Building PYZ because out00-PYZ.toc is non existent
2376 INFO: Building PYZ (ZlibArchive) /mnt/proj/build/test/out00-PYZ.pyz
2562 INFO: Building PYZ (ZlibArchive) /mnt/proj/build/test/out00-PYZ.pyz completed successfully.
2635 INFO: checking PKG
2635 INFO: Building PKG because out00-PKG.toc is non existent
2635 INFO: Building PKG (CArchive) out00-PKG.pkg
5005 INFO: Building PKG (CArchive) out00-PKG.pkg completed successfully.
5031 INFO: Bootloader /usr/lib/python2.7/site-packages/PyInstaller-3.3.1-py2.7.egg/PyInstaller/bootloader/Linux-64bit/run
5031 INFO: checking EXE
5031 INFO: Building EXE because out00-EXE.toc is non existent
5032 INFO: Building EXE from out00-EXE.toc
5034 INFO: Appending archive to ELF section in EXE /mnt/proj/dist/test
5074 INFO: Building EXE from out00-EXE.toc completed successfully.
生成的可执行文件在dist目录中。
[root@localhost proj]# cd dist
[root@localhost dist]# ls
test
[root@localhost dist]# ./test
print the result of 'ls -al':
total 4480
drwxrwxrwx 1 root root 144 Jul 6 18:25 .
drwxrwxrwx 1 root root 4096 Jul 6 18:26 ..
-rwxrwxrwx 1 root root 4581952 Jul 6 18:25 test
References:
https://www.pyinstaller.org/downloads.html
https://blog.csdn.net/aaronjzhang/article/details/8778338
https://www.cnblogs.com/LarryGen/p/5427102.html
https://www.cnblogs.com/alan-babyblog/p/5147770.html
https://www.cnblogs.com/zflibra/p/4180796.html