5

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.

5
  • 2
    run convert.exe in an already open command prompt and post the output. Commented Aug 16, 2022 at 16:01
  • @Harsh I just tried and it's the exact same thing... Commented Aug 16, 2022 at 16:25
  • 4
    I won't create an account on Vimeo to watch your video... Commented Sep 15, 2022 at 15:57
  • 1
    the behavior you want is not as simple as you think, let me explain it slowly, when you run it using your IDE , it will be using a certain python interpreter inside a certain environment, both the interpreter and the environment change when you compile a file to an executable, the executable don't just have to "run pyinstaller" it has to set up the environment correctly and has to connect to a different python interpreter that it going to execute pyinstaller ... this is not as straight forward as you may think, nor portable to another computer, just substitute the executable with a .bat file. Commented Sep 16, 2022 at 12:50
  • it's not impossible to do, it just needs someone to write an entire library that is going to do all the required wiring at runtime, which is extensive, and there are easier approaches to compiling applications at runtime, for applications that actually require compiling things at runtime, which are usually very specific. Commented Sep 16, 2022 at 13:05

2 Answers 2

3
+25

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

Sign up to request clarification or add additional context in comments.

2 Comments

I'm not sure I understood what you meant by the "one-dir distribution", if u meant replacing onefile with onedir in the command it would create a hole folder that the .exe is dependent to, it can't work without it, and I do not want to have this issue. And a user input isn't a possible solution cause the cause the converting thing is just a small part of a bigger program where at the end of the execution a python script is created and then should be converted a an executable automatically, with any user interaction.
You interpreted correctly what I meant with onedir, yes. Sorry this did not work for you. You said you auto-create a python script which then should be converted. Is it perhaps possible to put the created script to the same folder everytime, then you can use an absolute path to this folder in your convert.exe. sounds horrible but in this specific case perhaps helps
1
  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.

1 Comment

1. is not a solution cause the program should do that on its own without the user's interaction for the conversion. 2. the path isn't the problem cause os.path.abspath("myFile.py") is the full path (os.path.abspath() adds the rest of the path to the "myFile.py"). 3. I want this to work on any computer, with or without python installed (so no pyinstaller installed too), that's why I want convert.py to be an executable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.