首页 > 解决方案 > 需要为 smartsheet-python-sdk 和 PyInstaller 创建一个钩子

问题描述

好的,这里有很多信息,但这是我为这个解决方案寻找几天的结果。我见过很多人以各种形式提出这个问题而没有好的答案,我想我真的很接近解决它了!

我有一个使用 smartsheet-python-sdk 模块的功能齐全的 python 脚本,但是当我将它作为与 Pyinstaller 捆绑的 exe 运行时,它会将 smartsheet 对象作为字符串读取,我无法访问任何属性。该模块有一个名为“models”的子文件夹和另一个名为“enums”的子文件夹。我想我需要创建一个挂钩来导入这些,所以我尝试构建一个,但仍然没有运气。编译时读取了这些钩子,但它们不起作用。

这里供参考的是smartsheet 包结构

系统信息

一切都是最新的当前版本:Python 3.7 Pyinstaller 3.6 Smartsheet 2.86

操作系统:Windows 10

迄今为止的尝试

有人在这篇文章中找到了问题的解决方案,但他们没有提供解决方案,所以没有多大帮助。我已尝试按照此处的建议添加导入语句

这是我为 smartsheet.models 创建的尝试钩子:

from PyInstaller.utils.hooks import collect_submodules

hiddenimports = collect_submodules('smartsheet.models')

一些可能的原因它不起作用

我认为这与模块初始化文件中的信息有关,但我不确定如何处理它。主模块的init有以下语句:

from .smartsheet import Smartsheet, fresh_operation, AbstractUserCalcBackoff  # NOQA

并且模型子模块的init具有导入目录中找到的各个模型的语句:

from __future__ import absolute_import

# import models into model package
from .access_token import AccessToken
from .account import Account
from .alternate_email import AlternateEmail
from .attachment import Attachment
from .auto_number_format import AutoNumberFormat
# This continues for other models

所以我认为我需要在我的钩子文件中以某种方式模仿这个模型导入语句,但我不知道该怎么做。

代码和错误信息:

它可以创建主智能表对象,因为它不引用任何子模块项:

# Creates a smartsheet object for an account with an api access token
ss = smartsheet.Smartsheet(a_token)

但是任何在这个对象中引用子模块的东西都会失败

ss.Sheets.get_sheet(residential_id)

这是我运行程序时收到的错误消息:

ImportError! Could not load api or model class Users

# print statement I added to show the string object that is supposed to be a smartsheet object
<smartsheet.smartsheet.Smartsheet object at 0x00000292D4232408> 

Exception type: AttributeError 

Exception message: 'str' object has no attribute 'get_sheet' 

Stack trace:  
 File: sum_report.py
    Line: 516
    Function nameName: main 
    Message: 

 File: snow_functions.py
    Line: 437
    Function nameName: connect_smartsheet 
    Message: 

标签: pythonpython-3.xhookpyinstallersmartsheet-api

解决方案


我遇到了同样的问题。我发现您必须将所需的所有单个模块添加为隐藏导入。

打开您创建的 .spec 文件。如果您以前使用过,请在您的脚本目录中pyinstaller script.py查找该文件。script.spec打开它并修改部分:

hiddenimports=[
    'smartsheet.models'
    'smartsheet.sheets',
    'smartsheet.search',
    'smartsheet.users'  
    ]

然后运行pyinstaller script.spec以使用带有隐藏导入的规范文件。再次尝试运行你的包。如果您的脚本再次失败,您可能需要将其他模块添加到隐藏的导入数组中(只需查看错误中引用的模块。)

添加模型、表格和搜索后,我终于开始工作了。


推荐阅读