Skip to main content

Command Palette

Search for a command to run...

How to --> Convert PY --> EXE

Published
β€’2 min read
How to --> Convert PY --> EXE
O

I'm a software developer based in the United Arab Emirates πŸ‡¦πŸ‡ͺ. I enjoy coding open-source projects that solve unique, out-of-the-box problems. I love combining my hobbies like design, coding and building various cool projects with Raspberry Pi and IoT. Obsessed with Automation and Software Development.

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**

A

This is an ok solution if you want to bundle it so that it can easily be distributed and run. However, don't think that this is safe or secure for your source code as you can easily use a tool like https://github.com/extremecoders-re/pyinstxtractor and retrieve all your python code. It is a great easy to follow article and I have used pyinstaller as well, it is just something to keep in mind.

O

Thanks for reading this article.

Actually this article was made for development purpose and not for production purpose, it was just made so curious people would know how to do that, but of course there are better ways for production.

Thank you and happy coding πŸ’›πŸ’™

M

Great guide! Very short and clear!

1
O

You're welcome Mattia Ducci, if you have any suggestion, you are welcome to tell it to me 😊.

Happy Coding! Best regards,

~ Omar