Why converting a .py file to a .exe file with an already converted executable file doesn't work?

Viewed 259

I have my convert.py file that converts myFile.py to myFile.exe, and that's working. But when I convert convert.py to convert.exe it won't convert myFile.py anymore.

All of these files are inside of one folder:

enter image description here

convert.py:

import PyInstaller.__main__
PyInstaller.__main__.run([os.path.abspath("myFile.py"), '--noconfirm', '--onefile', '--console', f'--icon={os.path.abspath("icon.ico")}', "--name", "myFile"])

myFile.py:

print("Hello World!")
while True:
    continue

When running convert.exe 2 folders should be created: build and dist, myFile.exe should be created inside of dist, but here's a video of what's happening, and here's a video of what should be happening.

2 Answers

Your problem

The problems at this point are the file locations.

When you run the convert.py it looks for the file myFile.py. This is found in the same directory and the path is forwarded to pyinstaller. myfile.exe is created and everything is fine.

When you create convert.exe via pyinstaller as a one-file distribution and then run it, there are different path locations:

(a) path to the convert.exe

(b) path where convert.exe is executed

When you start convert.exe (in path (a) ) the content is extracted to a temporary directory and executed there (path (b) ). As in this path (b) no file myFile.py exists this will not work.

Possible solutions:

one-dir distribution

create a one-dir distribution of your convert.py. To avoid always searching for the *.exe in the distribution directory create a batch-file at the parent directory which will step in the subfolder and executes the *.exe for you. Also, change the search path of myfile.py to ../myfile.py in your code, so that it looks in the parent directory of the *.exe (the same path as the batch-file)

user input

for this solution you can use a one-file distribution of your convert.py. While executing the convert.exe just ask the user for the absolute path of the file which should be converted

  1. Try running a command from CMD Administrative
  2. Use the full path when calling the pyinstaller
  3. Uninstall pyinstaller, and install it again

command for

pyinstaller ./myFile.py --onefile --icon=icon.ico --noconsole --clean -D -F

If none of the above works, then try using py2exe. It is similar to pyinstaller here is the guide.

Related