After coverting .py into .exe

I installed “pyinstaller”, and converted a .py file into .exe, and now, when I open the .exe file, it is opened, but when I write a word in front of the input option and press Enter, the cmd page shuts down immediately.

Also, when I try to open “pip.exe” in the scripts folder of python, it is opened for seconds, but before its content appears, it shuts down instantly.

I suspect that the issue is related to whatever is in the .py script, but that’s only a best guess, as I’ve no idea what you’ve written, as the code.

Maybe you could post that?


Hi, Rob

The code is a short one and it works in the editor properly and there is no problem in the code.

Also, if we suppose the problem is due to the code failure, why does the default scripts of python (pip.exe, pip2.exe in the scripts folder of python) are shut down less than a few seconds after I open them?

This the code I converted into an excitable file ( of course, I myself added if and else to the code):

input('your table:')
if table:
    table = {'Sally': 4127, 'James': 4098, 'Wagner': 7678}
for name, phone in table.items():
     print(f'{name:10} ==> {phone:10d}')
    print('the input is wrong')

Sorry, you’ve lost me here, because to me, this reads as…

“This the code I converted into an excitable file”:

input('your table:')

for name, phone in table.items():
     print(f'{name:10} ==> {phone:10d}')

Is that correct?


My guess is…

These and console aka terminal programs,
You cannot double click them.
You need to start cmd.exe and then type path to your .exe at the command prompt.


Oh, you deleted " if " function that is conditional!

There is a probability that it doesn’t work?

The original code that I got from tutorial is this code:

   table = {'Sally': 4127, 'James': 4098, 'Wagner': 7678}
for name, phone in table.items():
     print(f'{name:10} ==> {phone:10d}')

Second, my request is not about the code itself. As I explained before, it is correct.
my problem is that the converted executable file (.exe) is closed unwanted.


The point to my questions, is that if the code (the Python script) throws some error, then it would explain why the .exe simply closes.

If you are sure that the script is bullet proof and can’t, under any condition, throw an exception, then fine, that’s not the issue and my guess was wrong.


You are right, dear Rob

I am confused. I examined it in the editor before converting, and as far as I remember, it worked correctly, but I don’t know why it got an error now!

Probably I have forgotten something in the code.

Of course, the code error can be corrected later, but now my problem is shutting down the .exe file

Barry Scott is right. I can’t open the file directly and should do that through cmd.exe.

I examined that way, and it worked but received a traceback error due to the error in the code (what you senior pointed out)



I’ve made similar mistakes in my time, but learning where we’ve gone wrong, makes us better coders.

Always go back and check everything, from the first line of code to the last, and never make assumptions.


I have another problem:

When I want to convert a .py file into .exe with “pyinstaller” the .py file should be presented in “system32” folder, also the output file is saved in the " C:\Windows\System32\dist" path instead of

What is the solution to change the path and specify a folder other than a folder in “system32”, whether in the python path or elsewhere ?

That is very unlikely to be a pyinstaller default.
Check how you configured pyinstaller to make sure you did not ask it to write to c:\windows etc,

I just installed it via cmd, but don’t know how it was configured.

This what I received from cmd by typing “pyinstaller” and “pyinstaller path”

For me it looks like you are not paying attention to some things which probably seem to be unimportant to you but they are very important in a program code.

For example here pay attention to the else: statement. It should probably follow a different statement than the for: block. Unfortunately (? :confused:) else: after a loop block is a valid syntax but it is an advanced and not very often used construct:

The following code is invalid. You cannot start a program with indentation:

It could be helpful if you link to the actual tutorial instead of the whole

The traceback you have shown (please paste it as text - also between triple backticks - next time) comes from a different code. Please always show the code and the traceback which belong together otherwise it is useless.

On Windows (and other systems) do not put your programs into system directories and do not run them as Administrator. This way you have a high chance of wrecking your system. pyinstaller probably uses the current working directory which could make you thinking that you have to use the system directory.

You should create a project directory in your home and work there.


Many thanks for the fine explanations.

The issue is that I don’t know how to change the default directory of pyinstaller into another one that is out of the system files directory. May you tell me what I should do?

Please check the documentation. I know nothing about pyinstaller but it seems that --distpath is the option you are looking for.

I do mean the config of the pyinstall software.
I mean the config you give to pyinstaller to make your .exe.

So what do you use to invert a python code into an application or an executable program?

Also I don’t know what to write in cmd along with “distpath DIR”?

You probably mean Spec Files?

I simply do not need that. :slight_smile:

Does it mean you don’t convert any python file at all, or do you mean you use another method?

I use Python on machines where it is pre-installed (like on many Linux distributions) or I install it there (like on Windows). So I do not need a self-contained executable.