0

Trying to include 3 files which are used in the script scriptName.

  1. JSON file.

  2. Driver file.

  3. Logo icon

Tried to do it with --add-data but this option is for non-code use files. So I tried spec file.

##specName.py##

block_cipher = None

added_files = 
         [
         ( 'jsonfile.json', '.D:\\Directory\\jsonfile'), 
         ( 'logo1.ico', '.D:\\Directory\\logo1'),
         ( 'chromedriver.exe', '.D:\\Directory\\chromedriver')
         ]


a = Analysis(['gui.pyw'],
             pathex=['D:\\Pyton\\...\\scriptName'],
             binaries=[],
             datas=added_files,
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='MyExcutable.exe',
          debug=False,
          strip=False,
          upx=True,
          console=False, icon='D:\\Pyton\\...\\logo1.ico' )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='gui')

finaly--pyi-build specName.spec then right after making one executable file

pyinstaller.exe --onefile --windowed --icon=logo1.ico scriptName.py

EDITED By the way, on the script I'm using this function for relative path -

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)

(It gets the orignial path)

But the exe does not work. Any ideas?

Omri
  • 673
  • 4
  • 9
  • 25
  • Please enable console(with `console=True` in spec file) and run your executable from a command line and put the full error log. – Masoud Rahimi Jul 22 '19 at 08:16

1 Answers1

0
  1. I think I invoked pyinstaller directly on the spec file like so pyinstaller.exe specName.spec. spec file states the script name, whether it's onefile etc. I'd guess the last line you wrote above actually produces a new spec file.

  2. As far as I remember, second element in tuples passed as datas parameter should hold a relative path (which will be interpreted / created relative to temporary folder to where the executable will "self-extract". See e.g. here on how to find these files/path afterwards. If you need to refer to an external file, you should use direct paths in your application's configuration / code. Alternatively, you can include it in the bundle and manually copy it to an external location (which seems a somewhat weird thing to do though).

  3. As the pyinstaller manual states, it may be worth to first debug in "one-directory" setting, including making sure all relevant files are included, and only then pass to onefile mode (which will be relatively straightforward at this point)

Yuri Feldman
  • 2,354
  • 1
  • 20
  • 23