From 74467d1d22b1a351d8e9160266f4aa02ded360a9 Mon Sep 17 00:00:00 2001 From: Rok Mandeljc Date: Mon, 13 Feb 2023 22:36:49 +0100 Subject: [PATCH] Add work-around for tensorflow < 2.3.0 trying to use site.USER_SITE Add a run-time hook for `tensorflow` that sets `site.USER_SITE` to an empty string, in order to prevent run-time errors caused by `tensorflow` < 2.3.0 blindly passing `site.USER_SITE` to `str.startswith`. Due to `site` being disabled in PyInstaller-frozen application, `site.USER_SITE` is `None`. In PyInstaller versions prior to v5.5, this was mitigated by PyInstaller's own fake `site` module, which also set `site.USER_SITE` to an empty string. --- news/546.update.rst | 3 +++ .../hooks/rthooks.dat | 1 + .../hooks/rthooks/pyi_rth_tensorflow.py | 19 +++++++++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 news/546.update.rst create mode 100644 src/_pyinstaller_hooks_contrib/hooks/rthooks/pyi_rth_tensorflow.py diff --git a/news/546.update.rst b/news/546.update.rst new file mode 100644 index 00000000..d4742874 --- /dev/null +++ b/news/546.update.rst @@ -0,0 +1,3 @@ +Add work-around for ``tensorflow`` < 2.3.0 trying to use +``site.USER_SITE``, which is ``None`` in PyInstaller 5.5 and later +due to removal of PyInstaller's fake ``site`` module. diff --git a/src/_pyinstaller_hooks_contrib/hooks/rthooks.dat b/src/_pyinstaller_hooks_contrib/hooks/rthooks.dat index bb92347f..25fd8edc 100644 --- a/src/_pyinstaller_hooks_contrib/hooks/rthooks.dat +++ b/src/_pyinstaller_hooks_contrib/hooks/rthooks.dat @@ -10,4 +10,5 @@ 'pythoncom': ['pyi_rth_pythoncom.py'], 'pyqtgraph': ['pyi_rth_pyqtgraph_multiprocess.py'], 'pywintypes': ['pyi_rth_pywintypes.py'], + 'tensorflow': ['pyi_rth_tensorflow.py'], } diff --git a/src/_pyinstaller_hooks_contrib/hooks/rthooks/pyi_rth_tensorflow.py b/src/_pyinstaller_hooks_contrib/hooks/rthooks/pyi_rth_tensorflow.py new file mode 100644 index 00000000..c967f489 --- /dev/null +++ b/src/_pyinstaller_hooks_contrib/hooks/rthooks/pyi_rth_tensorflow.py @@ -0,0 +1,19 @@ +#----------------------------------------------------------------------------- +# Copyright (c) 2023, PyInstaller Development Team. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# +# The full license is in the file COPYING.txt, distributed with this software. +# +# SPDX-License-Identifier: Apache-2.0 +#----------------------------------------------------------------------------- + +# `tensorflow` versions prior to 2.3.0 attempt to use `site.USER_SITE` in path/string manipulation functions. +# As frozen application runs with disabled `site`, the value of this variable is `None`, and causes path/string +# manipulation functions to raise an error. As a work-around, we set `site.USER_SITE` to an empty string, which is +# also what the fake `site` module available in PyInstaller prior to v5.5 did. +import site + +if site.USER_SITE is None: + site.USER_SITE = ''