Python: how to specify output folders in Pyinstaller .spec file

Question:

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

Asked By: Nickj

||

Answers:

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
Answered By: Jayaprakash

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… 🙂

Answered By: TocToc
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.