简体   繁体   中英

how pyinstaller exe file work on other computers with included files

I'm trying to make an executable file, using pyinstaller, with included files (json, logo, driver) which will work on any computer.

I've included several files in the executable through this line -

pyinstaller.exe --onefile --add-data "jsonfile.json;." --add-data "chromedriver.exe;." --add-data "logo1.ico;." --windowed --icon=logo1.ico script.py

SUCCESS BUT, when I opened it on another computer, does not work.

Also, I moved the files from the current directory on my computer and also not working - so something to do with the path I assume.

This code is for paths for any file included -

def resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)

To that function, the path on my computer is sent. Any ideas what went wrong?

4 Things, none of which actually solve your problem, but might help

  1. put all your assets in an asset subfolder
  2. explicitly call out the target --add-data "data/jsonfile.json;./data/jsonfile.json"
  3. you can try something like os.startfile(sys._MEIPASS) to open the folder and look at it (note you should put a sleep or a pause after because it will clean up the files when the program crashes or closes)
  4. when you call resource path make sure you call it with the relative path resource_path("data/myfile.json")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM