I'm working on a project with wxPython, which includes a non-standard font. On Windows there's no problem, as I can just use wx.AddPrivateFont
, however, macOS (OS X) does not support this, and the font has to be already installed. I've found that this can be done by placing the file in the users Fonts directory like this: (I'm of course making sure that this will only run under OS X)
user_font_directory = f"/Users/{getpass.getuser()}/Library/Fonts"
shutil.copyfile(path, f"{user_font_directory}/{file_name}")
This actually works, and any program that runs after this, is able to use the font. However, the program that installed it is itself not able to use it before it is restarted manually, as there seems to be some sort of font cache that is only given to a process right when it's launched. To get around this i firstly tried just reinitializing the wx.App
, but this made no difference. I then tried making Python restart itself using this method:
def restart_program():
python = sys.executable
os.execl(python, python, *sys.argv)
But still to no avail. I then tried separating the installation itself into another script, which after completion launched the main script as a sub process:
subprocess.run([sys.executable, "main.py"])
This had no effect.
So is there a way to make Python programmatically reload the font cache from OS X (or is there a better solution altogether)?