1

I want to execute an exe file from a python file that is compiled using pyinstaller

I'm using the following code:

import subprocess, os, sys

def resource_path(relative_path):
    """ Get absolute path to resource, works for dev and for PyInstaller """
    try:
        # PyInstaller creates a temp folder and stores path in _MEIPASS
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

new_path = resource_path("executable.exe")
    
print new_path
subprocess.Popen(new_path)

And I compile it using:

pyinstaller --add-binary executable.exe;exe -F incluse.py

Which creates incluse.exe and If I execute it I get the following error:

C:\Users\MyUsername\AppData\Local\Temp\_MEI13~1\executable.exe
Traceback (most recent call last):
  File "incluse.py", line 16, in <module>
  File "subprocess.py", line 394, in __init__
  File "subprocess.py", line 644, in _execute_child
WindowsError: [Error 2] The system cannot find the file specified
[21812] Failed to execute script incluse

What I want it to do is execute the executable.exe that I included, which should come up with a message box.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
hetijav
  • 159
  • 2
  • 12
  • ...executable files are not python modules. You can't just "import" them. With pyinstaller you can specify what files shouldbe copied in your "dist" folder, so you could copy executable.exe to an always-constant-relative-to-the-python-script location and use the relative path in your script – GPhilo Feb 07 '19 at 09:39
  • @GPhilo I know, thats why I asked the quistion. I dont know how to use a executable function in a python script – hetijav Feb 07 '19 at 09:41
  • See my edited comment (hit enter before finishing typing) – GPhilo Feb 07 '19 at 09:41
  • What does the executable do? maybe there is a simple python lib for that? There are huge number of libraries. I think this is XY problem. See: http://xyproblem.info/ – bhathiya-perera Feb 07 '19 at 09:48
  • @BhathiyaPerera I already explained that you need to give the executable an argument, and whatever the argument is, comes out of the computers speakers. – hetijav Feb 07 '19 at 09:58
  • Right it says for example. So I thought actual executable did something else. See: https://stackoverflow.com/questions/48438686/realistic-text-to-speech-with-python-that-doesnt-require-internet – bhathiya-perera Feb 07 '19 at 10:09
  • @BhathiyaPerera Sorry for causing any confusion. I ment with "for example" that you could change what it said It could've said bye to or any argument you give it – hetijav Feb 07 '19 at 10:13
  • I fully changed my quistion please see what you can do @BhathiyaPerera – hetijav Feb 07 '19 at 10:58
  • @hetijav what is preventing you from using a python text to speech library and avoid the .exe? – bhathiya-perera Feb 07 '19 at 11:40
  • What are the files in C:\Users\MyUsername\AppData\Local\Temp\_MEI13~1\ – bhathiya-perera Feb 07 '19 at 11:41
  • I think there should be a executeable.exe there because of the add-binary function I used to compile the script, but the whole Temp_MEI thing doesnt seem to be there – hetijav Feb 07 '19 at 11:44

1 Answers1

3

You can bundle another binary into your exe with pyinstaller using the --add-binary option.

In your Python script you can then call the exe embedded within your exe by using subprocess.Popen(exe_path). You can use sys._MEIPASS to access the temporary location that the exe will be found at in order to build the path to the exe.

Example

putty_launcher.py

import os
import sys
import subprocess

if getattr(sys, 'frozen', False):
        base_path = sys._MEIPASS
else:
        base_path = ""

exe_path = os.path.join(base_path, 'binaries\putty.exe')

subprocess.Popen(exe_path)

Folder Structure

root
├── binaries
│   └── putty.exe
├── putty_launcher.py

In the root folder, execute:

pyinstaller --add-binary "binaries\putty.exe;binaries" --onefile putty_launcher.py

This will then build an exe from the putty_launcher.py script which can successfully call the version of putty.exe that is embedded within the exe.

adambro
  • 310
  • 4
  • 16
  • Could you give an example code? I dont fully understand how this is done. – hetijav Feb 07 '19 at 09:48
  • @hetijav I've edited the answer to include an example of this approach. Hope this is helpful. – adambro Feb 07 '19 at 13:16
  • @hetijav I've noticed the question has changed quite a bit since I first answered so you have got really close to my answer now anyway. The only problem it appears you have is that your path to the executable.exe is wrong. Your --addbinary executable.exe;exe is putting the executable.exe in a folder called exe within the temporary folder that is created when the exe is run. This is the equivalent of "binaries" in my answer. So you only need to add "exe" to the path you are calling the executable.exe with. – adambro Feb 07 '19 at 13:29
  • thanks for your awsner, and it works, but if I change the pyinstaller to `pyinstaller --add-binary "binaries\hello.exe;binaries" --onefile script.py` and I change the 10th line to `exe_path = os.path.join(base_path, 'binaries\hello.exe')` and I change my name of the script to script.py and change putty.exe to hello.exe, and I compile it, I get the following error: `WindowsError: [Error 2] The system cannot find the file specified [26264] Failed to execute script script` – hetijav Feb 07 '19 at 15:40
  • @hetijav I'm afraid I wasn't able to reproduce this error so I'm not sure where the problem is. – adambro Feb 07 '19 at 19:57
  • 1
    I know how to reproduce the error. if you change the name of the exe to anything that starts with an "n", you would need to change the 10th line to `exe_path = os.path.join(base_path, 'binaries\no.exe')` can u already see it? Now we have \n wich python sees as a new line. So the way to fix this was to put r before the binaries path variable – hetijav Feb 08 '19 at 07:37