Where should I create temporary files intended to be used by programs which may be packaged as snaps?

I am writing a python program which programmatically creates a temporary file and then runs a command on it. By default, the Python tempfile module creates temporary files in /tmp. Unfortunately this path is not available to snaps.

What is a suitable, standardized temporary location to create my temporary file in order to retain compatibility with both snap-packaged and non-snap-packaged versions of the command?

  • Dropping files in a random subdirectory of the user’s home directory is not suitable. The user’s home directory is not a temporary directory and should not be treated as such.
  • Dropping the file anywhere in ~/snap is also not suitable, as this is not compatible if the software is not packaged with snap.
  • ~/.cache is not suitable, as the contents of this directory is also not available to snaps.