how to compile python code in exe on windows or linux

How to compile Python code to .exe


A bit of theory

Python is a high-level General-purpose programming language focused on improving developer productivity and code readability.

I would like to note that Python is one of the most interesting and powerful programming languages for me. I met him around 2016, and only after a year did I realize all his power and beauty.

Many people wonder when they wrote a program in Python: “How can I compile it to a .exe file?” . The question is quite complex, for someone who has just discovered this language and has not yet encountered pip.

pip is a package management system that is used for installing and managing software packages that are written in Python. In short, pip is a Python file Manager.

pyinstaller is a program that collects all dependencies and python applications in one package and turns it into an executable file for Windows, Linux, and MacOS.

Work with exe files using python programming language

Let’s figure out how to compile Python code to .exe in a minute, using a terminal(on Linux) or cmd(on Windows).

First, we need to install pyinstaller using pip. Writing a command on the command line:

pip install pyinstaller

If you are using Linux and don’t have pip installed, just write the command:

apt-get install python-pip

On Windows, if you haven’t changed the Python installation configuration, you shouldn’t have a problem with not having pip.

The second step is to go to the directory with our project. Just copy the path and use the command cd (our path) to go to the directory, for Linux and Windows the command is the same.

Now let’s compile our Python code to .exe, let’s not delay.

How to compile Python code in .exe

We will do this using the installed pyinstaller package.

How to compile Python code in .exe - image 2

Writing a command:

pyinstaller -F -w -i( to set up icon on your .exe)

FAQ about python & exe files

Let’s take each flag apart, you won’t necessarily need all of them:

How many flags I need to know when use pyinstaller?

You need to know just 3 flags.

What flags I need to know to compile python code into exe file?

-F – this flag is responsible for ensuring that the created dist folder, in which our executable file will be stored, does not contain too many unnecessary files, modules, etc.
-w – you will need this flag if the application uses tkinekt, it blocks the creation of a console window, but if your application is a console window, you do not need to use this flag.
-i – this flag is responsible for installing the icon on our executable file, after the flag you need to specify the full path to the icon with its name. For Example: D:\LayOut\icon.ico

How long does compilation take?

It all depends on the project, but it usually happens instantly or in a couple of minutes.

Who will be able to open the file?

Everyone who use Windows. You don’t even need Python on your computer to do this.

That’s all, today we looked at how to compile Python code in .exe, If you still have any questions, write them in the comments, I will be happy to answer.

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like