Python:如何在Pyinstaller.spec fi中指定输出文件夹

2024-05-16 02:27:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python 3.5和pyinstaller3.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),我希望将它们存储在其他地方,而不是默认目录中。有人知道如何在spec文件中设置这些文件夹的位置吗?我希望我的命令行命令尽可能简单,即.exe应该只通过键入

pyinstaller SCADAsync_spec.spec

在Pyinstaller手册中,我似乎可以将名为“DISTPATH”和“workpath”的全局变量指定给spec文件(https://pythonhosted.org/PyInstaller/spec-files.html)。但我真的不知道怎么做。

任何帮助都将不胜感激!

尼克


Tags: 文件nonefalsetruesourcerepoanalysisblock
2条回答

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

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

正如乔纳斯·格里格指出的那样,你确实可以做到:

import PyInstaller.config
PyInstaller.config.CONF['workpath'] = "./my_build_directory"

# ... rest of spec file

但是,配置模块中的文档指出,它只对有限数量的变量起作用:

This module holds run-time PyInstaller configuration.

Variable CONF is a dict() with all configuration options that are necessary for the build phase. Build phase is done by passing .spec file to exec() function. CONF variable is the only way how to pass arguments to exec() and how to avoid using 'global' variables.

NOTE: Having 'global' variables does not play well with the test suite because it does not provide isolated environments for tests. Some tests might fail in this case.

NOTE: The 'CONF' dict() is cleaned after building phase to not interfere with any other possible test.

To pass any arguments to build phase, just do:

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

And to use this variable in the build phase:

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

This is the list of known variables. (Please update it if necessary.)

cachedir

hasUPX

hiddenimports

noconfirm

pathex

ui_admin

ui_access

upx_dir

workpath

如果在大写字母中使用“DISTPATH”,则技巧将不起作用(Pyinstaller 3.3.1和python 2.7.13),但如果将其设置为小写:

import PyInstaller.config
PyInstaller.config.CONF['distpath'] = "./my_app_directory"

# ... rest of spec file

那会有用的。。。:)

相关问题 更多 >