【问题标题】:Python: how to specify output folders in Pyinstaller .spec filePython:如何在 Pyinstaller .spec 文件中指定输出文件夹
【发布时间】:2016-09-16 03:19:14
【问题描述】:

我正在使用 python 3.5 和 pyinstaller 版本 3.1.1。我指定了一个名为 SCADAsync_spec.spec 的 .spec 文件,如下所示:

block_cipher = None

a = Analysis(['SCADAsync.py'],
             pathex=['C:\\repo\\analysis\\trunk\\source\\python\\functions', 'C:\\repo\\analysis\\trunk\\source\\python\\Executables'],
             binaries=None,
             datas=[('figs\\ROMO_icon.ico','figs'),('figs\\OpenFile2.gif','figs'),('figs\\ROMOWind_Logo2015_CMYK.png','figs')],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)

pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='SCADAsync',
          debug=True,
          strip=False,
          upx=True,
          console=True
       )

使用

执行时效果很好
pyinstaller SCADAsync_spec.spec

现在创建了两个大文件夹(dist 和 build),我更愿意将它们存储在默认目录之外的其他位置。有谁知道如何在规范文件中设置这些文件夹的位置?我想让我的命令行命令尽可能简单,即 .exe 应该只通过键入来构建

pyinstaller SCADAsync_spec.spec

从 Pyinstaller 手册看来,我可以为规范文件 (https://pythonhosted.org/PyInstaller/spec-files.html) 指定名为“DISTPATH”和“工作路径”的全局变量。但我真的不知道该怎么做。

任何帮助将不胜感激!

尼克

【问题讨论】:

    标签: python pyinstaller


    【解决方案1】:

    Pyinstaller spec/build/dist 位置路径可以配置为 pyinstaller 命令的一部分。参考下面的例子

    pyinstaller --specpath /opt/bk/spec --distpath /opt/bk/dist --workpath /opt/bk/build testscript.py
    

    【讨论】:

    • 感谢您的回复。我有两个问题: 1. 有没有办法将这些路径合并到 .spec 文件中? 2. 这些也可以是绝对路径还是必须是相对路径?
    • 位置可以是绝对路径或相对路径,但不确定是否将路径配置作为规范文件本身的一部分。
    • @Nickj:有。 import PyInstaller.config; PyInstaller.config.CONF['spec'] = "value"
    【解决方案2】:

    正如 Jonas Gröger 指出的那样,您确实可以做到:

    import PyInstaller.config
    PyInstaller.config.CONF['workpath'] = "./my_build_directory"
    
    # ... rest of spec file
    

    但是配置模块中的文档说它只适用于有限数量的变量:

    此模块包含运行时 PyInstaller 配置。

    变量 CONF 是一个包含所有必要配置选项的 dict() 对于构建阶段。通过将 .spec 文件传递​​给 exec() 来完成构建阶段 功能。 CONF 变量是如何将参数传递给 exec() 和 如何避免使用“全局”变量。

    注意:拥有“全局”变量并不能很好地与测试套件配合使用 因为它没有为测试提供隔离环境。一些测试可能 在这种情况下失败。

    注意:'CONF' dict() 在构建阶段后被清理以不干扰 任何其他可能的测试。

    要将任何参数传递给构建阶段,只需执行以下操作:

    from PyInstaller.config import CONF
    CONF['my_var_name'] = my_value
    

    在构建阶段使用这个变量:

    from PyInstaller.config import CONF
    foo = CONF['my_var_name']
    

    这是已知变量的列表。 (如有必要,请更新。)

    缓存目录

    有UPX

    隐藏进口

    没有确认

    pathex

    ui_admin

    ui_access

    upx_dir

    工作路径

    如果你在大写中使用“DISTPATH”,这个技巧将不起作用(Pyinstaller 3.3.1 和 python 2.7.13),但如果你将它设置为小写:

    import PyInstaller.config
    PyInstaller.config.CONF['distpath'] = "./my_app_directory"
    
    # ... rest of spec file
    

    这会起作用的...... :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-17
      • 2018-06-18
      • 1970-01-01
      • 2012-04-14
      相关资源
      最近更新 更多