0

I am trying to generate one .exe file with pyinstaller

my python file

import subprocess
proc=r".\file.exe"

CLI_VERSION=subprocess.check_output([proc, '-v'],shell=True).decode('utf-8').strip()
print (CLI_VERSION)

with file.exe being in the same folder as the python file python myfile.py works just fine and prints the expected output

Now when I try to package that as .exe and include file.exe, subprocess still fails to find the file

pyinstaller.exe --onefile --add-data ".\file.exe;." .\myfile.py

now take the generated.exe and try to run it:

.\file.exe' is not recognized as an internal or external command, ... ... ... subprocess.CalledProcessError: Command '['.\file.exe', '-v']' returned non-zero exit status 1.

I tried add-binary instead of add-data since this is an exe file but it is still not working. Please note that the file.exe always returns 0, if it is called properly.

I am assuming this has to do with how subprocess works? is there a way to get it to work?

Is there a way to list files included in the .exe package? Judging by the filesize variation, I think the file.exe has been added, but I believe subprocess access the filesystem directly without passing by the files included in .exe package, is that the case?

I tried to do add-data for some files then inside the python file do "dir" (ls equivalent of ls) I don't see the files I included with --add-data in the list

Edit: I unpackaged the generated .exe using pyinstxtractor. The outcome is that my file.exe is well included in the same place as the name_of_my_python_file.pyc in the extracted package.

This makes me believe more in my theory: subprocess accesses the filesystem directly and does not read inside the packaged data. Does anyone have the knowledge to confirm and suggest a workaround (if possible)?

Thanks

user206904
  • 504
  • 4
  • 16
  • it seems that file.exe runs but returns 1 which your program doesn't tolerate. What does `file.exe -v` return and what should be its return code? – Jean-François Fabre Jan 09 '23 at 14:43
  • `file.exe -v` returns the version. In this very particular example with my file it is 0.96, It doesn't. When I put the `file.exe` next to myfile.exe (compiled python) things work as expected. Which implied that either the file is not included properly in the packaged python file, or that subprocess is unable to read files included within the compiled .exe file. – user206904 Jan 09 '23 at 15:00
  • return code is not what's printed on the screen. If `-v` option quits with returncode != 0 it can make your subprocess fail if subprocess doesn't expect it. And `check_output` doesn't work if the underlying process returns 1. – Jean-François Fabre Jan 09 '23 at 15:07
  • it could be a problem of current directory too. Using `.\file.exe` is dangerous as you may not be in the directory you think you are – Jean-François Fabre Jan 09 '23 at 15:15
  • yeah but, with pyinstaller, I do "mount" the file to `.` , isn't that what `--add-data ".\file.exe;."` is supposed to do? – user206904 Jan 09 '23 at 15:19
  • pyinstaller embeds the file all right, and the file is executed all right. Can you drop `shell=True` (because it's useless and dangerous too) and see what happens? – Jean-François Fabre Jan 09 '23 at 15:24
  • Thanks for trying to help. When I remove it, i get : `FileNotFoundError: [WinError 2] The system cannot find the file specified `, I add it again, it becomes the error seen above – user206904 Jan 09 '23 at 15:34
  • 2
    https://stackoverflow.com/questions/51060894/adding-a-data-file-in-pyinstaller-using-the-onefile-option you probably need to run `os.path.join(sys._MEIPASS,"file.exe")` – Jean-François Fabre Jan 09 '23 at 16:36
  • @Jean-FrançoisFabre MERCI! that was it! – user206904 Jan 09 '23 at 17:55

0 Answers0