0

I've built a hangman game with the use of other .txt files in the same directory containing the word banks for the hangman game, these are opened from within the hangman.py file, when i use pyinstaller to package i use path\to\pyinstaller.exe hangman.py --onefile but this doesn't allow the use of the .txt files from within the executable. How do i package the .txt files so the executable can run properly?

p0seidon
  • 21
  • 9

1 Answers1

0

You can use

pyinstaller --add-data 'path/to/file.txt:path/inside/exe' hangman.py

or editing the spec file with a list of files, i.e.

added_files = [
     ( 'src/README.txt', '.' )
     ( '/mygame/sfx/*.mp3', 'sfx' ),
     ]
a = Analysis(...
     datas = added_files,
     ...
     )
Merig
  • 1,751
  • 2
  • 13
  • 18
  • if there are multiple txt files, the first option wouldnt work, right? and after editing the spec file to i have to rerun the pyinstaller command? or would it work regardless if the executable isnt in the same machine? – p0seidon Feb 23 '19 at 23:22
  • After you edit it, you rerun pyinstaller on the spec file: pyinstaller myscript.spec – Merig Feb 23 '19 at 23:31
  • when editing the spec file is it an absolute path? or relative path? i apologize for the trivial questions, im new to the whole packaging files other than .py with pyinstaller – p0seidon Feb 24 '19 at 00:13
  • You can use relative paths, starting from the root directory – Merig Feb 24 '19 at 09:18