compile mycli by pyinstaller

怎甘沉沦 提交于 2020-03-15 13:56:33

背景 

在服务器没有安装mycli,安装的python版本不一致的情况下,想到通过pyinstaller编译出mycli的二进制文件直接运行。

pyinstaller -F /usr/bin/python3.6/bin/mycli --onefile 编译成功,然后悲剧的情况来了

Traceback (most recent call last):
  File "mycli", line 11, in <module>
  File "site-packages/click/core.py", line 829, in __call__
  File "site-packages/click/core.py", line 782, in main
  File "site-packages/click/core.py", line 1066, in invoke
  File "site-packages/click/core.py", line 610, in invoke
  File "site-packages/mycli/main.py", line 1053, in cli
  File "site-packages/mycli/main.py", line 118, in __init__
  File "site-packages/configobj.py", line 554, in __getitem__
KeyError: 'main'
[277772] Failed to execute script mycli

问题解决

阅读mycli源码,发现问题出在配置读取部分

site-packages/mycli/main.py文件中

PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__))default_config_file = os.path.join(PACKAGE_ROOT, 'myclirc')

问题就出现在了__file__的使用上,mycli会读取site-packages/mycli/main.py同级目录下的myclirc, AUTHORS, SPONSORS三个文件,但是pyinstaller编译时,并不会打包这三个文件,导致运行出错。

接下来就是打包,让我们看看pyinstaller --add-data参数的使用方法

format: {source}{os_separator}{destination}

destination file or files: desination folder which will contain your source files

NOTENOT destination file name, desination folder path, which is RELATIVE path of distination root, NOT absolute path

这里相对路径的含义比较有意思,上面的报错信息路径为:site-packages/mycli/main.py,但是mycli/main.py中打印出的path确是/tmp/_MEItHovkQ/mycl

基于以上信息,我们可以这样编译mycli

#!/bin/sh

pyinstaller -F /usr/local/tencent/python3.6/bin/mycli --onefile \
--add-data "/usr/local/tencent/python3.6/lib/python3.6/site-packages/mycli/myclirc:mycli" \
--add-data "/usr/local/tencent/python3.6/lib/python3.6/site-packages/mycli/AUTHORS:mycli" \
--add-data "/usr/local/tencent/python3.6/lib/python3.6/site-packages/mycli/SPONSORS:mycli" \

pyinstaller与__file__ 

#测试代码#main.py
#!/usr/bin/python3

import aaa
import os

print(os.path.abspath(os.path.dirname(__file__)))


# aaa/__init__.py
import os
print(os.path.abspath(os.path.dirname(__file__)))
#直接执行脚本

root@b1f294ad4d44 ~# python3 main.py
/root/aaa
/root

#pyinstaller编译后执行

root@b1f294ad4d44 ~# ./dist/main
/tmp/_MEIqn3ZG5/aaa
/root

可见,入口python文件其实就是编译出来的二进制文件,所有引用的lib根目录是 /tmp/xxx/<lib-name>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!