News

New paper!

Wednesday, April 15, 2020

Python version issue with Tkinter, Tcl/Tk, and set correct paths for PyInstaller

Note: Things needed to figure out to run PyInstaller properly
PyInstaller recommends using python version management tools such as pyenv. I used pyenv

-1. To make tcl-tk installed with homebrew work with python installed by pyenv. If tcl-tk hasn't been installed, install tck-tk with homebrew as well (that's explained in the link below as well.)
https://qiita.com/survivor7777777/items/5a8e23d30822437ae9f9

0. To use python installed with pyenv, uninstall the version of python with pyenv you want to use for PyInstaller, and the version needs to be installed as follows:
$PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.6
https://qiita.com/kanedaq/items/8bb33c135fd89991d548

1. Needed to edit these files following this https://github.com/krid-sdnera/pyinstaller/commit/9b118288d52ea7207692937a0341a3b9b3199fa3
[Path to the required files as a reference]
/Users/yukasuzuki/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks/hook-_tkinter.py
/Users/yukasuzuki/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/loader/rthooks/pyi_rth__tkinter.py

(Path to the required files before installing pyenv)
/usr/local/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks/hook-_tkinter.py
/usr/local/lib/python3.7/site-packages/PyInstaller/loader/rthooks/pyi_rth__tkinter.py

2. To solve path issue in PyInstaller with pyenv
/Users/yukasuzuki/.pyenv/versions/3.7.6/lib/python3.7/site-packages/PyInstaller/utils/hooks/__init__.py

def relpath_to_config_or_make(filename):
    """
    The following is refactored out of hook-sysconfig and hook-distutils,
    both of which need to generate "datas" tuples for pyconfig.h and
    Makefile, under the same conditions.
    """

    # Relative path in the dist directory.
    prefix = _find_prefix(filename)
    rel_path = os.path.relpath(os.path.dirname(filename), prefix)
    if '../' in rel_path and is_darwin:
        new_path = os.path.relpath(os.path.abspath(rel_path), os.path.expanduser('~'))
    else:
        new_path = os.path.relpath(os.path.dirname(filename), prefix)
    #return os.path.relpath(os.path.dirname(filename), prefix)
    return new_path

3. Add binary files when running pyinstaller as option
The command I used (after installing pyenv) is
pyinstaller --noconfirm --log-level=WARN --nowindowed --add-binary '/System/Library/Frameworks/Tk.framework/Tk:.' --add-binary '/System/Library/Frameworks/Tcl.framework/Tcl:.' --paths='/Users/yukasuzuki/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/hooks' --additional-hooks-dir pyinstaller-hooks --runtime-hook /Users/yukasuzuki/.pyenv/versions/3.7.6/Python.framework/Versions/3.7/lib/python3.7/site-packages/PyInstaller/loader/rthooks/pyi_rth__tkinter.py --onefile GUI_SEIR_corona.py

pyinstaller --noconfirm --log-level=WARN --nowindowed --add-binary '/System/Library/Frameworks/Tk.framework/Tk:.' --add-binary '/System/Library/Frameworks/Tcl.framework/Tcl:.' --paths='/Users/yukasuzuki/.pyenv/versions/3.7.6/lib/python3.7/site-packages/PyInstaller/hooks' --additional-hooks-dir pyinstaller-hooks --runtime-hook /Users/yukasuzuki/.pyenv/versions/3.7.6/lib/python3.7/site-packages/PyInstaller/loader/rthooks/pyi_rth__tkinter.py GUI_SEIR_corona.py