How to --> Convert PY --> EXE

ยท

2 min read

One of the most frequent asked questions from Python Programmers

'How to Convert PY to EXE?'

The most simplest answer just to say, it is possible, and you can do it, in just a few moments and clicks!

For todays example, I am having the following Python File :)

My Python File is called main.py

name = input('What is your name?')
age = input('What is your age?')
hobby = input('What is your hobby?')
print('Hello ' + name + '! You are ' + age + ' years old. Your hobby is ' + hobby + '.')

So, let us get into it

pyinstaller

1. So first, locate the folder your Python file is located in. And type in the path powershell and press enter.

1st.PNG

2. Enter this command in PowerShell

pip install pyinstaller

image.png

Then pyinstaller should install, I installed it before.

3. In the PowerShell Window, write the following command & press enter, edit the file name to avoid errors

So for example

pyinstaller --onefile filename.py

This is an example, so, because my python file name is main.py, I will enter the following command

pyinstaller --onefile main.py

After the command is executed we should see this message

image.png

And in our folder, we find some new folders too

image.png

4. Go into the dist folder. You will find the exe with your file name. Like main.exe.

image.png

Double-click the exe, and it should run as a Python File.

Possible errors:

When you run the exe, it runs, but when it ends, it closes instantly, so you cannot read the result, to fix the issue, end your Python Code with

input()

So, you have reached the end of this blog, I hope you liked it, don't forget to smash that like button, and see you in the next!

**Happy Coding! Best Regards,

~ Omar**

ย