【问题标题】:How to import the package being frozen in the PyInstaller spec file?如何导入 PyInstaller 规范文件中被冻结的包?
【发布时间】:2021-02-24 06:44:38
【问题描述】:

我有这个目录结构:

mypackage/
|___ installer.spec
|___ package/
    |___ __init__.py

并且package.__init__.py 包含一些我想在我的installer.spec 文件中使用的变量。 所以这个文件包含一个import package 行。 但是,在 mypackage/ 中运行 pyinstaller installer.spec 失败并显示 ModuleNotFoundError

如何在使用 PyInstaller 冻结时从要冻结的包中检索变量?

【问题讨论】:

    标签: python import pyinstaller python-import


    【解决方案1】:

    在spec文件中,改变

    import package
    
    foobar = package.foobar
    

    import importlib.util
    
    spec = importlib.util.spec_from_file_location(
        "package", "/full/path/to/package/__init__.py"
    )
    package = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(package)
    foobar = package.foobar
    

    参考:How to import a module given the full path?

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 2020-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      相关资源
      最近更新 更多