Converting a Python file into an exe

Say you’ve written a Python program and you want to convert it into an exe and you want the simplest way of going about it.

At this point, there are probably a lot of options out there, but let’s consider two of them — 
1. Py2exe
2. PyInstaller

1) Using Py2exe is very simple and straightforward. Here’s an example -

Imagine you have a Python file that copies a file from one directory to another contingent on its presence in said directory —

Then this file say “copy.py” can be converted into an exe using Py2Exe by first generating a setup.py file for it.

from distutils.core import setup
import py2exe
setup(console=['copy.py'])

Finally, run —

python setup.py py2exe

This generates a copy.exe and a python27.dll that is required for its execution.
Now , you could add a option to the file — “ ‘bundle_files’ :1”, but this does not work on x64 based programs.

from distutils.core import setup
import py2exe, sys, os

setup(
options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
console= ['copy.py'],
zipfile = None,
)

And this reason is why you have option number 2 -

2) PyInstaller

Installing it is as easy as running — pip install pyinstaller

And making the executable just involves running — pyinstaller.exe -onefile copy.py

Ta-da! It’s done. Now you can just run your copy.exe from any location as a standalone executable.

Show your support

Clapping shows how much you appreciated Sinewaver’s story.