Alexander
Alexander

Reputation: 99

How to run exe file from python script converted to exe

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?

Upvotes: 3

Views: 1423

Answers (4)

buran
buran

Reputation: 14273

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()

Upvotes: 4

raspiduino
raspiduino

Reputation: 681

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.

Upvotes: 1

PythonSnek
PythonSnek

Reputation: 574

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.

Upvotes: 0

Allen Shaw
Allen Shaw

Reputation: 1492

Maybe you should try to use absolute path instead.

Upvotes: 0

Related Questions