144

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

Setup.py and oldlogs.py (this file need coversion)

setup.py code is

from distutils.core import setup
import py2exe

setup(console=['oldlogs.py'])

How can I convert oldlogs.py to an exe file?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Dewal Tewari
  • 1,449
  • 2
  • 10
  • 3
  • 1
    see it's [tutorial](http://www.py2exe.org/index.cgi/Tutorial) – Ashwini Chaudhary Sep 09 '12 at 13:57
  • 4
    Possible duplicate of [How to make a Python script standalone executable to run without ANY dependency?](http://stackoverflow.com/questions/5458048/how-to-make-a-python-script-standalone-executable-to-run-without-any-dependency) – Cees Timmerman May 18 '17 at 14:11
  • For completeness, [cx_Freeze](http://cx-freeze.sourceforge.net/) is another tool that you can use for this (along with PyInstaller and py2exe, which other answers have already mentioned). – Thomas K Oct 05 '12 at 11:42

4 Answers4

79

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:

pyinstaller oldlogs.py

From the tool's documentation:

PyInstaller analyzes myscript.py 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.

Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57
zenpoy
  • 19,490
  • 9
  • 60
  • 87
  • 9
    [cx_Freeze](http://cx-freeze.sourceforge.net/) is better, it supports even python 3.3. – Ashwini Chaudhary Sep 09 '12 at 14:03
  • Hey, this even works with [wine](http://winehq.org), at least up to version 3.4 of python, which supports Windows XP. Really great! I created a windows executable with **wine**, **python 3.4** and **pip-Win** (just follow the installation instructions on pyinstaller website) on my Fedora Linux machine. – erik Apr 28 '16 at 06:07
  • 3
    @AshwiniChaudhary, Both seem to be cross platform.. however, if I had to quickly judge which one to use just based on `GitHub` stars, `PyInstaller` has ~4000 stars, `cx_Freeze` has 200 stars. So `PyInstaller` seems to be more popular and probably has more edge cases covered. _why do you think `cx_Freeze` is better than `PyInstaller`?_ – alpha_989 Feb 25 '18 at 01:33
  • Note that compared to PyInstaller, "cx_Freeze does not support building a single file exe, where all of the libraries for your application are embedded in one executable file." https://cx-freeze.readthedocs.io/en/latest/faq.html#single-file-executables – Taylor D. Edmiston Jan 24 '20 at 13:45
16

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

utils/Makespec.py [--onefile] oldlogs.py

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/Build.py oldlogs.spec

More can be found here

pirho
  • 11,565
  • 12
  • 43
  • 70
uhz
  • 2,468
  • 1
  • 20
  • 20
10

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).

jotap
  • 488
  • 6
  • 9
  • 2
    I also recommend PyOxidizer, it is cross platform, zero-copy strategy and modern. See https://pyoxidizer.readthedocs.io/en/stable/pyoxidizer_comparisons.html for more details on comparisons with other tools. – Donghua Liu Dec 07 '21 at 00:55
-2
# -*- mode: python -*-

block_cipher = None

a = Analysis(['SCRIPT.py'],
             pathex=[
                 'folder path',
                 'C:\\Windows\\WinSxS\\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_50c6cb8431e7428f',
                 'C:\\Windows\\WinSxS\\x86_microsoft-windows-m..namespace-downlevel_31bf3856ad364e35_10.0.17134.1_none_c4f50889467f081d'
             ],
             binaries=[(''C:\\Users\\chromedriver.exe'')],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          name='NAME OF YOUR EXE',
          debug=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=True )
Xooonas
  • 17
  • 3