21

I am using python 3.5 and pyinstaller version 3.1.1. I have specified a .spec file, called SCADAsync_spec.spec, as follows:

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
       )

That works fine when executed with

pyinstaller SCADAsync_spec.spec

Now that creates two large folders (dist and build) , which I would prefer to store elsewhere than in the default directory. Does anyone know how to set the location of those folders in the spec file? I'd like to keep my command line command as simple as possible, i.e. the .exe should build just by typing

pyinstaller SCADAsync_spec.spec

From the Pyinstaller manual it seems I can specify globals called 'DISTPATH' and 'workpath' to the spec file (https://pythonhosted.org/PyInstaller/spec-files.html). But I cannot really figure out how to do that.

Any help would be greatly appreciated!

Nick

Nickj
  • 982
  • 3
  • 8
  • 17

2 Answers2

22

Pyinstaller spec/build/dist location paths can be configured as part of pyinstaller command. Refer below example

pyinstaller --specpath /opt/bk/spec --distpath /opt/bk/dist --workpath /opt/bk/build testscript.py
Jayaprakash
  • 703
  • 6
  • 15
  • 1
    Thanks for your reply. I have two questions: 1. Is there no way to incorporate these paths in the .spec file? 2. Can these also be absolute paths or do they need to be relative? – Nickj May 23 '16 at 07:25
  • Location can be of both absolute or relative paths, whereas am not sure of incorporating path configuration as part of spec file itself. – Jayaprakash May 23 '16 at 12:26
  • 4
    @Nickj: There is. `import PyInstaller.config; PyInstaller.config.CONF['spec'] = "value"` – Jonas Gröger Mar 29 '17 at 12:00
  • If you want to use an already generated spec file, remove the `--specpath` option and put the path to the spec file at the end, instead of the path to the py script, like: `pyinstaller --distpath /opt/bk/dist --workpath /opt/bk/build testscript.spec` – Aelius Mar 28 '23 at 09:03
16

As Jonas Gröger pointed out you can indeed do:

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

# ... rest of spec file

However the documentation in the config module says that it works only on a limited number of variable:

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

If you use "DISTPATH" in capital the trick will not work (Pyinstaller 3.3.1 and python 2.7.13) but if you set it lower case:

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

# ... rest of spec file

That will work... :)

Aelius
  • 1,029
  • 11
  • 22
TocToc
  • 4,739
  • 4
  • 29
  • 34