Python: 如何在 Pyinstaller .spec 文件中指定输出文件夹
Python: how to specify output folders in Pyinstaller .spec file
我正在使用 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 手册看来,我可以将名为 'DISTPATH' 和 'workpath' 的全局变量指定到规范文件 (https://pythonhosted.org/PyInstaller/spec-files.html)。但我真的不知道该怎么做。
如有任何帮助,我们将不胜感激!
尼克
Pyinstaller spec/build/dist 位置路径可以配置为 pyinstaller 命令的一部分。参考下面的例子
pyinstaller --specpath /opt/bk/spec --distpath /opt/bk/dist --workpath /opt/bk/build testscript.py
正如 Jonas Gröger 指出的那样,您确实可以做到:
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
那行得通...:)
我正在使用 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 手册看来,我可以将名为 'DISTPATH' 和 'workpath' 的全局变量指定到规范文件 (https://pythonhosted.org/PyInstaller/spec-files.html)。但我真的不知道该怎么做。
如有任何帮助,我们将不胜感激!
尼克
Pyinstaller spec/build/dist 位置路径可以配置为 pyinstaller 命令的一部分。参考下面的例子
pyinstaller --specpath /opt/bk/spec --distpath /opt/bk/dist --workpath /opt/bk/build testscript.py
正如 Jonas Gröger 指出的那样,您确实可以做到:
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
那行得通...:)