Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 754
Nickj Avatar asked May 19 '16 10:05

Nickj


People also ask

How do I create a folder in PyInstaller?

At least for pyinstaller 4.2 the datas field need to be added as a tuple. If you want to add multiple folders it needs to be something like: ... datas = [('test/dir', 'test/dir'),('test2/dir', 'test2/dir')] ...

Where is the .spec file in PyInstaller?

the first thing PyInstaller does is to build a spec (specification) file myscript. spec . That file is stored in the --specpath directory, by default the current directory.

What is Pathex in PyInstaller?

Explanation. pathex is an optional list of paths to be searched before sys.path. Source: https://github.com/pyinstaller/pyinstaller/blob/develop/PyInstaller/building/build_main.py#L123. The spec file is actually executable Python code. PyInstaller builds the app by executing the contents of the spec file.


2 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
like image 68
Jayaprakash Avatar answered Sep 18 '22 18:09

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... :)

like image 29
TocToc Avatar answered Sep 17 '22 18:09

TocToc