3

I have two EXE files, both are actually python scripts, converted using pyinstaller ( Using Auto Py to Exe ).

file1.exe : is the app. file2.exe : is license app, which checks the license, if ok it executes file1, if not it exits.

My problem is that when I merge the two exe files to be in 1 exe file as a result, file2.exe can not find file1.exe unless I copy it to the same directory.

pyinstaller command:

pyinstaller -y -F --add-data "D:/test/file1.exe";"."  "D:/test/file2.py"

Now I should have something like this:

file2.exe 
      +------- file1.exe

But each time I run file2.exe it gives me this error

WinError 2] The system cannot find the file specified: 'file1.exe'

till I copy file1.exe to the same directory (as it neglecting that I already merged it in pyinstaller) so the files tree looks like this:

file1.exe
file2.exe 
      +------- file1.exe

the command line in file2 to launch file1 is :

  os.system('file1.exe')

How can I fix that?

Alexander
  • 99
  • 7
  • 1
    Show your code that uses the file. This look like problem with the path in your script. check https://pyinstaller.readthedocs.io/en/stable/runtime-information.html#using-sys-executable-and-sys-argv-0 – buran Mar 14 '21 at 15:04
  • Is there a reason you don't bundle both Python scripts into one .exe? It seems like that would run more efficiently than bundling one, then bundling it into the other (once you get your path working). – CrazyChucky Mar 14 '21 at 15:07
  • @CrazyChucky, Yes as the second file as mentioned is checking the license. so it would be way more easier to use this approach. – Alexander Mar 14 '21 at 15:10
  • You'll still be running two entire Python runtimes. – CrazyChucky Mar 14 '21 at 15:10
  • @buran, Hi, I already provided the command like in file2.exe while execute the file1.exe. – Alexander Mar 14 '21 at 15:10
  • If you not copy the file1.exe to the dir, can you find where the pyinstaller file2.exe extract the file1.exe? It is %temp%? – raspiduino Mar 14 '21 at 15:22
  • @raspiduino, actually am not sure, how can I find out ? – Alexander Mar 14 '21 at 15:24

4 Answers4

4

when you do os.sysetm('file1.exe') it expects to find it in the current working directory from which you run your file2.exe

so, you need to do the following in order to use the file1.exe bundled in file2.exe.

import sys, os
if getattr(sys, 'frozen', False):
    # we are running in a bundle
    bundle_dir = sys._MEIPASS
else:
    # we are running in a normal Python environment
    bundle_dir = os.path.dirname(os.path.abspath(__file__))

os.system(os.path.join(bundle_dir, 'file1.exe'))

As I already mentioned in the comment, check Run-time Information section in PyInstaller docs.

As a side note, not related to your question, it's better to use subprocess.run(), instead of os.system()

buran
  • 13,682
  • 10
  • 36
  • 61
  • Thank you so much, this solved my problem. regarding your side note, can you please clarify why is subprocess.run() better ? – Alexander Mar 14 '21 at 15:35
  • 1
    I think it's because it doesn't start a new environment, and starts it as a subprocess. – PythonSnek Mar 14 '21 at 15:36
  • 1
    It is mentioned in the docs for `os.system` - *The [subprocess](https://docs.python.org/3/library/subprocess.html) module provides more powerful facilities for spawning new processes and retrieving their results; **using that module is preferable to using this function.*** – buran Mar 14 '21 at 15:38
1

From this post and this answer, I think you should change the PyInstaller command to pyinstaller -y -F --add-data "D:/test/file1.exe;." "D:/test/file2.py" instead of "D:/test/file1.exe";"."

In case it does not work, try pyinstaller -y -F --add-data "D:/test/file1.exe;file1.exe" "D:/test/file2.py" and the command to launch file1.exe will be os.system('main/file1.exe') if you put file2.exe in your PyInstaller distribute folder.

raspiduino
  • 601
  • 7
  • 16
0

Maybe you should try to use absolute path instead.

Allen Shaw
  • 1,164
  • 7
  • 23
0

Unless you know the absolute path for the files to be in C:\ for example you will not be able to do that, as the user account name is different for different people. I highly recommend combining them into one file, as that will most likely simplify the debugging as well. I am not sure if searching directories using something like pathlib or shutil will work, but it might find the wrong files.

PythonSnek
  • 542
  • 4
  • 21
  • They're already combining into one file with the `-F` option, but the path they're using to try to access the bundled .exe isn't working. – CrazyChucky Mar 14 '21 at 15:18
  • No I mean file1.exe and file2.exe. So just put the code from the app after the license check into one file and use that. It will leave just a single file.exe and will not encounter any reference issues – PythonSnek Mar 14 '21 at 15:20