How to compile python script to binary executable

Posted on

Question :

How to compile python script to binary executable

I need to convert a Python script to a Windows executable.

I have Python 2.6 installed to python26.

I have created one script and kept it in C:pythonscript. Inside this folder there are two files and (this file need coversion) code is

from distutils.core import setup
import py2exe


How can I convert to an exe file?

Answer #1:

Or use PyInstaller as an alternative to py2exe. Here is a good starting point. PyInstaller also lets you create executables for linux and mac…

Here is how one could fairly easily use PyInstaller to solve the issue at hand:


From the tool’s documentation:

PyInstaller analyzes and:

  • Writes myscript.spec in the same folder as the script.
  • Creates a folder build in the same folder as the script if it does not exist.
  • Writes some log files and working files in the build folder.
  • Creates a folder dist in the same folder as the script if it does not exist.
  • Writes the myscript executable folder in the dist folder.

In the dist folder you find the bundled app you distribute to your users.

Answered By: zenpoy

Answer #2:

I recommend PyInstaller, a simple python script can be converted to an exe with the following commands:

utils/ [--onefile]

which creates a yourprogram.spec file which is a configuration for building the final exe. Next command builds the exe from the configuration file:

utils/ oldlogs.spec

More can be found here

Answered By: uhz

Answer #3:

Since other SO answers link to this question it’s worth noting that there is another option now in PyOxidizer.

It’s a rust utility which works in some of the same ways as pyinstaller, however has some additional features detailed here, to summarize the key ones:

  • Single binary of all packages by default with the ability to do a zero-copy load of modules into memory, vs pyinstaller extracting them to a temporary directory when using onefile mode
  • Ability to produce a static linked binary

(One other advantage of pyoxidizer is that it does not seem to suffer from the GLIBC_X.XX not found problem that can crop up with pyinstaller if you’ve created your binary on a system that has a glibc version newer than the target system).

Overall pyinstaller is much simpler to use than PyOxidizer, which often requires some complexity in the configuration file, and it’s less Pythony since it’s written in Rust and uses a configuration file format not very familiar in the Python world, but PyOxidizer does some more advanced stuff, especially if you are looking to produce single binaries (which is not pyinstaller’s default).

Answered By: jotap

Answer #4:

# -*- mode: python -*-

block_cipher = None

a = Analysis([''],
                 'folder path',
pyz = PYZ(a.pure, a.zipped_data,
exe = EXE(pyz,
          name='NAME OF YOUR EXE',
          console=True )
Answered By: Xooonas

Leave a Reply

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