pyi_rth_mplconfig.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2013-2023, PyInstaller Development Team.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: Apache-2.0
  10. #-----------------------------------------------------------------------------
  11. # matplotlib will create $HOME/.matplotlib folder in user's home directory. In this directory there is fontList.cache
  12. # file which lists paths to matplotlib fonts.
  13. #
  14. # When you run your onefile exe for the first time it's extracted to for example "_MEIxxxxx" temp directory and
  15. # fontList.cache file is created with fonts paths pointing to this directory.
  16. #
  17. # Second time you run your exe new directory is created "_MEIyyyyy" but fontList.cache file still points to previous
  18. # directory which was deleted. And then you will get error like:
  19. #
  20. # RuntimeError: Could not open facefile
  21. #
  22. # We need to force matplotlib to recreate config directory every time you run your app.
  23. def _pyi_rthook():
  24. import atexit
  25. import os
  26. import shutil
  27. import _pyi_rth_utils.tempfile # PyInstaller's run-time hook utilities module
  28. # Isolate matplotlib's config dir into temporary directory.
  29. # Use our replacement for `tempfile.mkdtemp` function that properly restricts access to directory on all platforms.
  30. configdir = _pyi_rth_utils.tempfile.secure_mkdtemp()
  31. os.environ['MPLCONFIGDIR'] = configdir
  32. try:
  33. # Remove temp directory at application exit and ignore any errors.
  34. atexit.register(shutil.rmtree, configdir, ignore_errors=True)
  35. except OSError:
  36. pass
  37. _pyi_rthook()
  38. del _pyi_rthook