HeoN
HeoN

Reputation: 249

cx_freeze issue with relative path on mac

I'm having issues with cx_freeze relative path logic on mac. I'm using python 3.3 and pyqt5 and building an app portable windows - mac.

The following simple script just loads an image into a QLabel as a QPixmap.

The code works fine both on windows and mac when launched from console, builds both on windows and mac. The exe works on windows without any problem. The app ( MacOSX ) does not load the image, same for the dmg.

I have a feeling it has something to do with relative path setting. The reason for this belief is the following: - if I try to load the .app the image will not appear - if I copy-paste the testimage.jpeg in the same folder where the .app is it will load the image.

I did try include_files in the setup with no results.

Can anyone help?

Here are the scripts: First the file image_insert_test.py:

import sys
from PyQt5.QtWidgets import (QApplication, QDialog,
        QErrorMessage, QLabel,  QWidget, QVBoxLayout )

from PyQt5.QtCore import pyqtSlot, QDir, Qt
from PyQt5 import QtGui



class Window(QDialog):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)



        self.fill_box2 = QLabel()
        pixmap = QtGui.QPixmap('testimage.jpeg')
        self.fill_box2.setPixmap(pixmap.scaled(100,100,Qt.KeepAspectRatio))


        # set the layout
        layout = QVBoxLayout()
        layout.addWidget(self.fill_box2)
        self.setLayout(layout)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = Window()
    main.setWindowTitle('test gui')
    main.show()
    sys.exit(app.exec_())

the file setup.py:

import sys
from cx_Freeze import setup, Executable

base = None
if sys.platform == 'win32':
    base = 'Win32GUI'


options = {
    'build_exe': {
        "includes": ["matplotlib.backends.backend_macosx"] ,
        "include_files" : ["testimage.jpeg"]
    }
}

executables = [
    Executable('image_insert_test.py', base=base)
]

setup(name='image_insert_test',
      version='0.1',
      description='Sample image_insert_test script',
      options=options,
      executables=executables
      )

and this is the log of the cx_freeze bdist_mac http://pastebin.com/77uU4Exr

Upvotes: 1

Views: 761

Answers (1)

HeoN
HeoN

Reputation: 249

a workaround is the following: instead of

pixmap = QtGui.QPixmap('testimage.jpeg')

place:

pixmap = QtGui.QPixmap(os.path.join(os.getcwd(),'image_insert_test-0.1.app/Contents/MacOS','testimage.jpeg')

this fishes the image path from the .app package but there has to be a more elegant way to do it.

Upvotes: 1

Related Questions