2

I have a big python project which I'm currently developing, and I want to make a .exe file that launches the main.py file. I have a file called ultimate_launcher.py which has the following code:

exec(open("main.py").read())

That is a solution I found somewhere on Stack Overflow. it runs fine as a .py file, but when I use pyinstaller to convert it to a .exe, it has a "Fatal" error,

Failed to execute script ultimate_launcher

I don't want the rest of my project to be put into the .exe as I am still in the process of development. So my question is how do I make a .exe that ONLY references the file main.py so when it gets updated, i don't have to remake the .exe file.

py

PythonSnek
  • 542
  • 4
  • 21

1 Answers1

1

I believe there is a method to achieve this detailed in this thread:

Python - create an EXE that runs code as written, not as it was when compiled

EDIT: This works

import subprocess
f = open("c:\\temp\\temp.py", "w")
f.write(open("main.py").read())
f.close()
subprocess.call("\"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37_64\\Scripts\\pyinstaller.exe\" --distpath \"c:\\temp\" c:\\temp\\temp.py " )
subprocess.call("C:\\temp\\temp\\temp.exe")
Michael Royston
  • 226
  • 1
  • 8
  • Thank you, but the solution there isn't great, as what I need is a "shortcut" to main.py, as there is a lot of import mess in the whole project. Is there any way to achieve that? – PythonSnek Sep 16 '20 at 23:23
  • I think it has to do with the compilation while running the exe. I was thinking maybe try having your exe first create the file as a .py file which loads main in - then run your pyinstaller as a command on that created .py file and trigger that new exe? – Michael Royston Sep 16 '20 at 23:27
  • Do you mean have it create a file that imports main and then make a .exe of that? – PythonSnek Sep 16 '20 at 23:47
  • Just a thought I am not able to test it out myself at this time. If you are still stuck when I get back to my PC I will try it myself. – Michael Royston Sep 16 '20 at 23:50
  • Still the same Fatal Error and if i run the .py file, it can't find the 'c:\\temp\\temp.py' directory in f = open("c:\\temp\\temp.py", "w") – PythonSnek Sep 17 '20 at 13:29
  • As long as you have a c:/temp folder it should create or overwrite that temp.py file. You may need to put the full path your main.py - mine worked due to relative location. – Michael Royston Sep 17 '20 at 17:07
  • Where could the Pyinstaller folder be located? My C:\Users\Vlad\AppData\Local\Programs\Python\Python38-32\Scripts folder doesn't have a pyinstaller folder. – PythonSnek Sep 17 '20 at 23:07
  • If you go to where your python.exe and search the scripts folder. – Michael Royston Sep 18 '20 at 01:21
  • subprocess.call("\"C:\\Users\\Vlad\\AppData\\Local\\Programs\\Python\\Python38-32\\Lib\\site-packages\\PyInstaller\" --distpath \"c:\\temp\" c:\\temp\\temp.py " ) PermissionError: [WinError 5] Access is denied – PythonSnek Sep 19 '20 at 01:06
  • the error seems to be coming from me referencing a directory instead of a file at the first subprocess.call(). What file should I put instead of the folder? – PythonSnek Sep 19 '20 at 17:51
  • Pyinstaller is the file not the folder. The backlash is to escape the quote and in Windows the .exe is implied – Michael Royston Sep 20 '20 at 15:12
  • Yes, that works, but I had to add the .exe on the end anyways. Could you edit the post for future people to know that the pyinstaller path is to the pyinstaller.exe, not the Pyinstaller directory – PythonSnek Sep 21 '20 at 11:12