brentvollebregt / auto-py-to-exe

Converts .py to .exe using a simple graphical interface
MIT License
3.95k stars 677 forks source link

symbols-images into pushbutton auto-py-to-exe #484

Closed shlomiz89 closed 5 months ago

shlomiz89 commented 5 months ago
          Regarding you're keeping all the parameters in the tool the same, I do not see why they wouldn't be there. 

Are you using the "The one-file Resource Wrapper" snippet that I provided? You haven't said anything about it? I believe those files are there but you're not looking for them in the right place. Simply running the exe and the images not appearing in the GUI is not enough proof to tell me they're not there.

What was your thorough testing?

In the help post I extensively link I state:

One file mode is a bit different, instead of putting all the files in a folder, it puts them in something like a zip file which is contained in the end executable. When you run the executable, the files contained internally are unpacked to a new temporary directory.

Also due to the files being unpacked to a new temporary directory on execution, the files that you modified added that were in the same directory as the executable will not be there on the next run because they are now in a different unknown folder.

The reason you need to use this extra bit of code is because a one-file exe will unpack all of it's contents to a new folder in the operating systems temporary directory. This means the current working directory initially set in the application will not be where the files have been unpacked to unlike one-directory. This is why using relative references will work in one-directory but not in one-file - you need to adjust for the fact that the root of the project is now somewhere different.

Lets do some checking where we actually look at the folder:

  1. Create this script:
import sys, os

def resource_path(relative_path):  # Snippet from the post you have read
    """ Get the absolute path to the resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

print('Path:', resource_path('.'))  # Print out the root path of the bundle
input()  # Wait for enter to be pressed - we need this so the one-file directory is not deleted until we stop the script
  1. Add all your image files in auto-py-to-exe and bundle the above script as one-directory and console-based.
  2. Run the output exe and it should print out a path, go to that path in your file explorer - your files should be somewhere in that folder or a sub-folder.
  3. Go back to the console window and press enter to end the script.
  4. Go back to auto-py-to-exe and add all your image files and bundle the above script again as one-file and console-based.
  5. Run the output exe and it should print out a path, go to that path in your file explorer - your files should be somewhere in that folder or a sub-folder.
  6. After you have looked at that folder, you can then go back to the console window and press enter to end the script.

Step 6 should prove that your images are coming through. Just because we are bundling a different script doesn't mean that this will change which files are output (aside from Python script).

Originally posted by @brentvollebregt in https://github.com/brentvollebregt/auto-py-to-exe/issues/121#issuecomment-687667654

Hi, i have the same problem. i did the steps you mentioned above, the exe file not showing my images. but the images exist in the directory ("one directory" option) if i run through Spyder it's work good

the python code written as following:

    self.enterPB = QtWidgets.QPushButton(self.centralwidget)
    self.enterPB.setGeometry(QtCore.QRect(50, 370, 61, 51))
    self.enterPB.setStyleSheet("")
    self.enterPB.setText("")
    icon = QtGui.QIcon()
    icon.addPixmap(QtGui.QPixmap("enter.jpg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    self.enterPB.setIcon(icon)
    self.enterPB.setIconSize(QtCore.QSize(61, 51))
    self.enterPB.setObjectName("enterPB")

python file exe file

github-actions[bot] commented 5 months ago

👋 Hi, just a reminder that if you haven't read the help post yet, give it a read to see if your issue is covered in it and make sure to follow the debugging section.

Also please note, as stated in the README, if your issue is only associated with your application and not auto-py-to-exe itself, please do not create an issue in this repository - instead, comment on the help post, video or create a new discussion.

brentvollebregt commented 5 months ago

I see you are referencing "enter.jpg" in your script - since this is a relative reference, it will resolve relatively to the current working directory, which will most likely not be where the file is actually being stored in the bundle output.

To fix this, you need to wrap file references with resource_path to correctly turn a relative path into an absolute path which will point to the correct file. For example,

icon.addPixmap(QtGui.QPixmap(resource_path("enter.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)

You can read more about this in the help post under "The one-file Resource Wrapper".

shlomiz89 commented 5 months ago

Thank you very much for fast answer

should i write this code into my code?

i mentioned this in the class before setupUi def

def resource_path(relative_path):
    import sys, os
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

and reference it in
icon.addPixmap(QtGui.QPixmap(resource_path("enter.jpg")), QtGui.QIcon.Normal, QtGui.QIcon.Off)

i receive an error - undefined name "resource_path"

brentvollebregt commented 5 months ago

Yes, you need to add that function yourself. Nothing provides it for you and this tool doesn't add anything to your application.

shlomiz89 commented 5 months ago

you the besttttt!!!!!!! thank you <3