Skip to content

Instantly share code, notes, and snippets.

@txoof
Last active January 10, 2020 20:11
Show Gist options
  • Save txoof/ad20106b4feae079e0bf0a3af73bbc56 to your computer and use it in GitHub Desktop.
Save txoof/ad20106b4feae079e0bf0a3af73bbc56 to your computer and use it in GitHub Desktop.
PyInstaller Notes

PyInstaller - Important Notes

Install PyInstaller

Install PyInstaller within the pipenv - this appears to resolve some path searching issues

PyInstaller 2018 Version has issues with virtual environments and specifically with venv 16.7.9 (used by pipenv). This may be resovled by the 9 Jan 2020 Version. The develop branch appears to solve the issue:

pipenv install -e git+https://github.com/pyinstaller/pyinstaller.git@develop#egg=PyInstaller

Useful commandline options:

Run PyInstaller from inside the pipenv shell: pyinstaller --clean --debug=all --noconfirm foo.spec

Relative Paths are Weird

Paths are calculated from the working directory from which the binary is called; relative paths are rooted in the current working directory.

Example:

$ pwd 
/home/foo/bar/
# execute a built pyinstaller script:
$ ./spam/myScript
# relative paths such as `./config.ini` are rooted in the current working directory:
# thus ./config.ini resolves to: /home/foo/bar/config.ini 
# instead of: ./home/foo/bar/spam/config.ini

.spec file

When in doubt, regenerate it!

binaries

Binary includes are mapped literally: binaries=[('./foo/bar/spam.bin', './foo')] Literally maps to dist/myProject/foo/spam.bin

If it is necessary to maintain the directory structure the code snip below can be added to the .spec file:

from pathlib import Path

def getPathTuples(path): 
    """recursively search `path` for files and create a tuple of the original relative path 
       and the destination path in the format: ('path/subDirA/subDir1/file1.bin', 'path/subDirA/subDir1/')
    """
    myPath = Path(path)
    allEntries = list()
    for entry in list(myPath.glob('*')):
        if Path.is_dir(entry):
            allEntries = allEntries + getPathTuples(entry)
        else:
            allEntries.append((str(entry), str(entry.parent)))
    return allEntries

binDirs = ['./fonts', './images']
binTuples = []
for each in binDirs:
    tuples =(getPathTuples(each))
    for tup in tuples: 
        binTuples.append(tup)
binTuples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment