linux下pyinstaller打包python脚本


公司很多项目是python开发的,所以有些脚本需要加密打包,更为关键的是涉及的第三方包安装很是繁琐。特别是在没有外网的局域网内,安装更为头疼。让人难过的是,最近才发现有这么个工具pyinstaller用于多平台打包, 官方文档

安装

安装非常简单,通过pip安装即可

pip install pyinstaller

使用

安装完成后,直接输入pyinstaller 脚本.py即可。默认情况下,会将脚本设计的第三方模块打包进一个单独的文件夹中,里面有多个文件。如果想打包为一个可执行文件,可以通过-F来做。
如:

[root@lei SanshiMonitor]# pyinstaller -F test.py
82 INFO: PyInstaller: 3.4
82 INFO: Python: 2.7.5
82 INFO: Platform: Linux-3.10.0-957.el7.x86_64-x86_64-with-redhat-7.6-Maipo
83 INFO: wrote /SanshiMonitor/test.spec
103 INFO: UPX is not available.
105 INFO: Extending PYTHONPATH with paths
['/SanshiMonitor', '/SanshiMonitor']
105 INFO: checking Analysis
111 INFO: Building because /SanshiMonitor/test.py changed
112 INFO: Initializing module dependency graph...
126 INFO: Initializing module graph hooks...
327 INFO: running Analysis Analysis-00.toc
367 INFO: Caching module hooks...
373 INFO: Analyzing /SanshiMonitor/test.py
2652 INFO: Loading module hooks...
2653 INFO: Loading module hook "hook-cx_Oracle.py"...
2879 INFO: Loading module hook "hook-encodings.py"...
3737 INFO: Looking for ctypes DLLs
3737 INFO: Analyzing run-time hooks ...
3744 INFO: Looking for dynamic libraries
4394 INFO: Looking for eggs
4394 INFO: Using Python library /lib64/libpython2.7.so.1.0
4398 INFO: Warnings written to /SanshiMonitor/build/test/warn-test.txt
4417 INFO: Graph cross-reference written to /SanshiMonitor/build/test/xref-test.html
4476 INFO: checking PYZ
4479 INFO: Building because toc changed
4479 INFO: Building PYZ (ZlibArchive) /SanshiMonitor/build/test/PYZ-00.pyz
4772 INFO: Building PYZ (ZlibArchive) /SanshiMonitor/build/test/PYZ-00.pyz completed successfully.
4818 INFO: checking PKG
4819 INFO: Building because toc changed
4819 INFO: Building PKG (CArchive) PKG-00.pkg
8061 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
8086 INFO: Bootloader /usr/lib/python2.7/site-packages/PyInstaller/bootloader/Linux-64bit/run
8086 INFO: checking EXE
8087 INFO: Building because toc changed
8087 INFO: Building EXE from EXE-00.toc
8088 INFO: Appending archive to ELF section in EXE /SanshiMonitor/dist/test
8122 INFO: Building EXE from EXE-00.toc completed successfully.

会在同级目录下生成一个dist和built文件夹,进入dist文件夹下找到可执行文件test,输入./test即可执行。

[root@lei dist]# ./test
asdf

可以将该文件复制到其他Linux环境下即可执行,无需安装其他第三方包。

遇到的问题

由于脚本中包含了MySQL-python第三方包用于操作MySQL数据库,该包是通过源码来安装的而不是通过pip直接安装的。所以在打包过程中没有报错,但是在执行的时候报:

[root@BigData dist]# ./test
Traceback (most recent call last):
  File "test.py", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 395, in load_module
    exec(bytecode, module.__dict__)
  File "build/bdist.linux-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>
  File "/usr/lib/python2.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 395, in load_module
    exec(bytecode, module.__dict__)
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 7, in <module>
  File "build/bdist.linux-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: /tmp/_MEI8MetS7/_mysql.so: cannot open shared object file: No such file or directory

提示无法导入mysql包,经过多次尝试–hidden-import=MySQLdb-python都无法解决。后来我把原来安装的包给卸载了,通过pip重新安装,既然成功了。如下:

卸载

pip uninstall MySQL-python

重新安装

pip install MySQL-python

终于成功了!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值