78

I wrote a script that will help a Windows user in her daily life. I want to simply send her the .exe and not ask her to install python, dlls or have to deal with any additional files.

I've read plenty of the stackoverflow entries regarding compiling Python scripts into executable files. I am a bit confused as there are many options but some seem dated (no updates since 2008) and none were simple enough for me not to be asking this right now after a few hours spent on this.

I'm hoping there's a better, up-to-date way to do this.

I looked into:

but either I couldn't get them to work or couldn't understand how to get the result I need. The closest I got was with py2exe but it still gave me the MSVCR71.dll

I would appreciate a step-by-step answer as I was also unable to follow some of the tweaking answers here that require some prior understanding of how to use py2exe or some of the other tools.

I'm using Python 2.5 as one of the modules is only available for that version.

1
  • 1
    most of the time you can ignore the MSVCR dll, I don't distribute it with my application and haven't heard any any errors (key point being they may have happened, and I just haven't had a report of it :)) Jan 26, 2010 at 0:50

8 Answers 8

33

PyInstaller will create a single-file executable if you use the --onefile option (though what it actually does is extracts then runs itself).

There's a simple PyInstaller tutorial here. If you have any questions about using it, please post them...

1
  • Its a lot better recently. You can just type pip install pyinstaller to install it and pyinstaller -w yourpythonscript.py to compile it to an exe.
    – G_Style
    May 21, 2019 at 16:07
13

Using py2exe, include this in your setup.py:

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

sys.argv.append('py2exe')

setup(
    options = {'py2exe': {'bundle_files': 1}},
    windows = [{'script': "YourScript.py"}],
    zipfile = None,
)

then you can run it through command prompt / Idle, both works for me. Hope it helps

13

i would recommend going to http://sourceforge.net/projects/py2exe/files/latest/download?source=files to download py2exe. Then make a python file named setup.py. Inside it, type

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

Save in your user folder Also save the file you want converted in that same folder

Run window's command prompt type in setup.py install py2exe

It should print many lines of code...

Next, open the dist folder.

Run the exe file.

If there are needed files for the program to work, move them to the folder

Copy/Send the dist folder to person.

Optional: Change the name of the dist folder

Hope it works!:)

2
  • This is awesome, I hade my scripts organised in several folders that were using each other, but this method picked up all of the dependencies. I've trued pyinstaller-2.0 and that didn't pickup dependencies causing the resulting executable to print errors related to importing the dependencies.
    – Aravind
    Aug 13, 2013 at 8:34
  • I tried to use this method but i didn't work. When i typed the info in command prompt it just ran my script
    – kflaw
    Sep 24, 2013 at 15:24
9

I would join @Nicholas in recommending PyInstaller (with the --onefile flag), but be warned: do not use the "latest release", PyInstaller 1.3 -- it's years old. Use the "pre-release" 1.4, download it here -- or even better the code from the svn repo -- install SVN and run svn co http://svn.pyinstaller.org/trunk pyinstaller.

As @Nicholas implies, dynamic libraries cannot be run from the same file as the rest of the executable -- but fortunately they can be packed together with all the rest in a "self-unpacking" executable that will unpack itself into some temporary directory as needed; PyInstaller does a good job at this (and at many other things -- py2exe is more popular, but pyinstaller in my opinion is preferable in all other respects).

2
  • I have looked up to the links you gave here, they are not available any more. Does PyIstaller work on more than a file and with Python 2.7 too? Jul 20, 2015 at 2:03
  • 2
    PyInstaller now lives on GitHub, it's up to version 2.1, and supports Python 2.7 (among other releases). Of course "it works on more than one file" (as input) but the whole point is producing a single file as output. See github.com/pyinstaller/pyinstaller/wiki and links therefrom. Jul 29, 2015 at 21:49
6

1) Get py2exe from here, according to your Python version.

2) Make a file called "setup.py" in the same folder as the script you want to convert, having the following code:

from distutils.core import setup
import py2exe
setup(console=['myscript.py']) #change 'myscript' to your script

3) Go to command prompt, navigate to that folder, and type:

python setup.py py2exe

4) It will generate a "dist" folder in the same folder as the script. This folder contains the .exe file.

1
  • 2
    Could you possibly delete this answer since it's a duplicate of Bob's answer below posted 2 years earlier.
    – sricks
    Jun 29, 2016 at 21:04
2

you may want to see if your app can run under IronPython. If so, you can compile it to an exe http://www.codeplex.com/IronPython

1
  • 1
    If you use IronPython (I'm developing a commercial software in it be aware that it requires .NET Framework 2.0 )
    – DCC
    Feb 26, 2010 at 20:29
1

You can create executable from python script using NSIS (Nullsoft scriptable install system). Follow the below steps to convert your python files to executable.

  • Download and install NSIS in your system.

  • Compress the folder in the .zip file that you want to export into the executable.

  • Start NSIS and select Installer based on ZIP file. Find and provide a path to your compressed file.

  • Provide your Installer Name and Default Folder path and click on Generate to generate your exe file.

  • Once its done you can click on Test to test executable or Close to complete the process.

  • The executable generated can be installed on the system and can be distributed to use this application without even worrying about installing the required python and its packages.

For a video tutorial follow: How to Convert any Python File to .EXE

-4

You could create an installer for you EXE file by:
1. Press WinKey + R
2. Type "iexpress" (without quotes) into the run window
3. Complete the wizard for creating the installation program.
4. Distribute the completed EXE.

1
  • 4
    This is not correct
    – sricks
    Jun 29, 2016 at 21:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.