pyinstaller

2019-10-08  本文已影响0人  D_Major

先pyinstaller .py生成.spec后再修改.spec文件, 然后pyinstaller .spec
-F生成单个文件, --key加密对汇编代码进行代码混淆
-p地址可以写到spec里的pathex, 然后对spec文件进行pyinstaller
pyinstaller -F --key=xxxxxx train_pixel_link.spec

修改python框架, 有两种方法, 目前有用的是第一种:

1.

选第一个要配合修改spec文件以查找.so并放入binaries中,
vim ~/.virtualenvs/tensorflow1.8/lib/python2.7/site-packages/tensorflow/python/framework/load_library.py
在load_op_library()函数前加入resource_path()函数

def resource_path(relative_path):
    import sys
    import os
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")
    path = os.path.join(base_path, relative_path)
    return path

然后在load_op_library()函数的第一行加上这一句以将tensorflow安装路径替换成临时路径, 这样在临时路径中就能查找到.so库了

library_filename = resource_path(library_filename.split('/')[-1]) # REMOVE AFTER PYINSTALLER USE

然后修改spec文件如下

# -*- mode: python ; coding: utf-8 -*-
block_cipher = pyi_crypto.PyiBlockCipher(key='960525')

import os

# 这段代码为了寻找tensorflow所有的.so库, 然后返回一个列表
tensorflow_location = '/home/renduo/.virtualenvs/tensorflow1.8/lib/python2.7/site-packages/tensorflow'
tensorflow_binaries = []
for dir_name, sub_dir_list, fileList in os.walk(tensorflow_location): 
  for file in fileList:
    if file.endswith(".so"):
      full_file = dir_name + '/' + file
      print(full_file)
      tensorflow_binaries.append((full_file, '.'))


a = Analysis(['train_pixel_link.py'],
             # 外部地址
             pathex=['/home/renduo/.virtualenvs/tensorflow1.8/bin', '/home/renduo/.virtualenvs/tensorflow1.8/lib', '/home/renduo/.virtualenvs/tensorflow1.8/lib/python2.7/site-packages/tensorflow/contrib', '/home/renduo/PycharmProjects/ZJU_pixellink/pylib/src/util'],
             # 外部.so库
             binaries=tensorflow_binaries,
             # 要传入的数据
             datas=[('dist/tensorflow', '.'),
            ('dist/tensorflow/contrib', '.'),
            ('pylib/src/util', '.')],
             # tensorflow.contrib懒加载
             hiddenimports=['tensorflow.contrib'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='train_pixel_link',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=True )

2.

选第二个要把so都拷到目录下(要和原来的目录结构对应, 如tensorflow/python), 然后放入datas传进去
vim ~/.virtualenvs/tensorflow1.8/lib/python2.7/site-packages/tensorflow/python/platform/resource_loader.py
修改get_path_to_datafile函数, 将原来的return _os.path.join(data_files_path, path)注释换成
# root = _os.path.dirname(_sys.executable) #pyinstaller
# return _os.path.join(root, path) #pyinstaller

然后用以下命令拷贝so, 也可以把整个contrib拷贝过去

cp -rf `find /home/renduo/.virtualenvs/tensorflow1.8/lib/python2.7/site-packages/tensorflow/contrib -name *.so` /home/renduo/PycharmProjects/ZJU_pixellink/dist/tensorflow/python/

存在的问题:

在临时目录sys._MEIPASS中找不到依赖的库, 尤其是pdb的cmd模块, 在IPython的debugger模块调用pdb时出现

上一篇 下一篇

猜你喜欢

热点阅读