0
votes

I created a package that contains an icon.png inside a resource-folder:

  • ../mypackage/resource/icon.png
  • ../mypackage/qtprogram.py

I am now trying to access the icon.png in qtprogram.py. As they are both in mypackage directory, I tried using:

QtGui.QIcon("resource//icon.png")

but it did not work. However, using the full path (in relation to the main script path) did. Since this should be a shareable package, I would like to avoid using the full path. How can I do this?

1
Relative paths are not relative to the script, but to the working directory. Use the pathlib module and get the path using Path(__file__).parent / 'resource' / 'icon.png'musicamante

1 Answers

0
votes

I think it should be \\ (or) r'relative-path-to-file' instead of // in QtGui.QIcon("resource//icon.png").

I would use the os library.

import os

# If you plan to use to without PyInstaller
icon_path = os.path.join(os.path.dirname(__file__), r'resources\icon.png') 

# If you plan to use to pyinstaller
icon_path = os.path.join(os.path.abspath(os.getcwd()), r'resources\icon.png')