How to Create EXE Files from Python Scripts

Python is a great programming language for the beginners and advanced users alike. It is available for multiple platforms and provides great support for all sorts of programming. But we can create only .py scripts using the Python language which can be run only when Python interpreter is installed on your computer.

What if we want to distribute the programs that we have developed using Python to the users who do not have Python interpreter installed? We can ask them to install Python on their computer, but a much practical solution is to create EXE executable files from the Python scripts.

For creating EXE from Python scripts, we can use py2exe in the following manner:

  1. First of all we have to install py2exe by using the command py -m pip install py2exepy2exe
  2. Create your Python script that you want to convert into EXE. We will use hello.py for these instructions which just prints a string on the screen.py2exe
  3. Create another Python script named setup.py in the same folder with the following contents. You should replace hello.py with the filename of your own Python script.py2exe
    from distutils.core import setup
    import py2exe
    setup(console=['hello.py'])
  4. Now run the command py setup.py py2exepy2exe
  5. You will find the EXE file in a sub-folder named dist. You can run it on any computer without really needing Python installed first.py2exe

All of this is possible because of open-source py2exe. The developer of py2exe provides all the resources and and examples that would be needed for anyone to learn how to effectively use it for converting Python scripts to EXE files.

Since Python is compiled using Microsoft Visual C++, all the programs created in the above manner also require the Visual C++ runtimes to be installed on any of the computers on which you want to run the created EXE files. You can find more details about py2exe from https://www.py2exe.org/index.cgi/Tutorial.