Skip to content
Advertisement

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement