I'm building a Python application and don't want to force my clients to install Python and modules. I also want to make my application closed-source.

So, is there a way to compile Python scripts to standalone executables?

share|improve this question
1  
It will be (very) difficult to make the application truly closed-source; for instance, I think you can unzip the exe produced by py2exe to get the original source files. – katrielalex Mar 28 '11 at 11:43
2  
No secure option to make your app closed source, .pyc and .pyo files are not that hard to revert. – neurino Mar 28 '11 at 12:10
95  
@S.Lott: Get real. Although the Python installer is fine for the computer literate, it would be ridiculous to ask Grandma to install Python on Windows just so she could run a Python recipe filer. She'd ditch the Python app and use something closed-source and easy to install instead. – Dave Nov 11 '11 at 11:53
196  
The part of the question that says "I don't want to force my clients to install Python and modules." It doesn't say the clients can't do it, it says the developer doesn't want to require them to do it. There's no point in debating the point here, because the clients are not reading SO to speak for themselves. I wouldn't want to install a Flash development environment just to watch youtube, or a C development environment just to play Sudoku, or a Python development environment to run a version of Angry Birds written in Python. That's what redistributable runtimes are for. – Dave Nov 11 '11 at 20:03
13  
@Dave How does this comment have more likes than both the question and the all the answers? ^_^ – Vedaad Shakib Jun 26 '14 at 5:31

15 Answers 15

up vote 126 down vote accepted

You can use py2exe as already answered and use cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so in linux, much harder to revert than common .pyo and .pyc files (and also gain in performance!)

share|improve this answer
    
Thanks for answer. I've considered Cython, but it seems it still need Python to run compiled executable. Do you mean compiling script to .pyc using Cython? (I didn't know if Cython has such feature) – Jeff Mar 28 '11 at 12:18
4  
I mean you have two problems: closed source and no-deps. For closed source compile your important code in C libs using Cython. For no-deps use py2exe and make an executable with .pyd (cython compiled) files within. – neurino Mar 28 '11 at 12:24
3  
What would you do about the libraries (numpy, scipy, matplotlib) ? For instance, how would clients be able to use the ginput() function in matplotlib from the executable, without having it installed on their computer. – chimpsarehungry Apr 10 '13 at 18:04
    
@chimpsarehungry what about static linking? – Braden Best Jan 28 '15 at 7:15
7  
Last release seem to be in 2008, is this still maintained ? – Zitrax Sep 17 '15 at 8:45

I have had some success with pyinstaller. Works on linux as well as windows.

share|improve this answer
2  
It's worth noting that pyinstaller can compile to a single .exe file. Also on which OS it is run, makes that executable, i.e. .exe when run on Windows – Richard de Wit Jun 6 '14 at 14:17
4  
sadly no support for 3.X – Itay Moav -Malimovka Nov 6 '14 at 16:43
1  
@ItayMoav-Malimovka They are working in the support of Python 3. See github.com/pyinstaller/pyinstaller/tree/python3. – PhoneixS Jan 13 '15 at 8:21
4  
using 'pyinstaller' is way easier than 'py2exe'. It takes care of every dependencies including numpy, pandas, anything and gives you a single executable on the working platform! – aks Mar 9 '16 at 5:00
3  
Seems like exactly what I was looking for. And now supports Python 3! Thanks :) – Daniel Gruszczyk Aug 30 '16 at 8:23

You might wish to investigate Nuitka. It takes python source code and converts it in to C++ API calls. Then it compiles into an executable binary (ELF on Linux). It has been around for a few years now and supports a wide range of Python versions.

You will probably also get a performance improvement if you use it. Recommended.

share|improve this answer
4  
Nuitka is an awesome tool. No need not to use it. I'd only point out one thing - it's not limited to Linux only - it's cross-platform - another reason why to use it. – sjaustirni Nov 20 '14 at 16:09
5  
Nuitka works on Python 2.6, 2.7, 3.2, 3.3, and 3.4, and is cross-platform:(Linux, FreeBSD, NetBSD, MacOS X, and Windows (32/64 bits). Others may work as well. Architectures: x86, x86_64 (amd64), and arm. Other architectures may also work, out of the box.) – Jonathan Hartley Mar 15 '15 at 1:33

And a third option is cx_Freeze, which is cross-platform.

share|improve this answer

you may like py2exe. you'll also find in there infos for doing it on linux

share|improve this answer
2  
But you will need to give it the package all option. – Jakob Bowyer Mar 28 '11 at 11:37

Use py2exe.... use below set up files:

 from distutils.core import setup
 import py2exe

 from distutils.filelist import findall
 import matplotlib

 setup(
       console=['PlotMemInfo.py'],

       options={
                'py2exe': {
                'packages' : ['matplotlib'],
            'dll_excludes': ['libgdk-win32-2.0-0.dll',
                                 'libgobject-2.0-0.dll',
                 'libgdk_pixbuf-2.0-0.dll']
                          }
                },
       data_files = matplotlib.get_py2exe_datafiles()
     )
share|improve this answer
1  
you saved my life – Veltro Aug 26 '15 at 16:39
    
You Welcome @Liam – Anand Aug 27 '15 at 6:38
    
I did this and it created a python27.dll file and it is still required. I moved the .exe file out of the folder holding the python27.dll file and the .exe file no longer runs. – mjwrazor Aug 16 '16 at 14:58
    
Once you move out of the folder it wont work. – Jesh Kundem Nov 14 '16 at 15:04

I would like to compile some useful information about creating standalone files on windows using Python 2.7.

I have used py2exe and it works, but I had some problems.

This last reason made me try PyInstaller http://www.pyinstaller.org/ .

In my opinion, it is much better because:

  • It is easier to use.

I suggest creating a .bat file with the following lines for example (pyinstaller.exe must be in Windows Path):

pyinstaller.exe --onefile MyCode.py

So, I think that, at least for python 2.7, a better and simpler option is PyInstaller.

share|improve this answer
1  
The --onefile flag won't help you to make it portable on the Windows, because the executable still wants to link with the api-ms-win-crt-runtime-l1-1-0.dll library (or/and others) which must be installed through the vcredist package. So a dependency is still there and must be installed before the run. – Andry Jul 13 at 11:13

I also recommend pyinstaller for better backward compatibility such as python 2.3 - 2.7.
for py2exe, you have to have python 2.6

share|improve this answer

Yes, it is possible to compile Python scripts into standalone executable.

PyInstaller can be used to convert Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX. It is one of the recommended converters.

py2exe converts Python scripts into only executable in Windows platform.

Cython is a static compiler for both the Python programming language and the extended Cython programming language.

share|improve this answer
1  
Thanks for this. I was using py2exe, and couldnt get the icons working with it at all. PyInstaller worked first time, and looks great! – Chud37 May 11 at 7:24

For Python 3.2 scripts the only choice is Cxfreeze. Build it from sources otherwise it won't work.

For python 2.x I suggest pyinstaller as it can package a python program in a single executable, unlike CxFreeze which outputs also libraries.

share|improve this answer

py2exe will make the exe file you want but you need to have the same version of MSVCR90.dll on the machine you're going to use your new exe. See http://www.py2exe.org/index.cgi/Tutorial for more info.

share|improve this answer

You can find the list of distribution utilities listed @ https://wiki.python.org/moin/DistributionUtilities.

I use bbfreeze and it has been working very well (yet to have python 3 support though).

share|improve this answer
    
stackoverflow.com/questions/33683133/… I use bbfreeze very nice package. – user914425 Dec 2 '15 at 19:59
    
Does it take care of dependencies and install relevant py modules? – Volatil3 May 9 '16 at 17:20
    
@Volatil3 Yes, all the dependencies are taken care of. WRT to installation of modules on the host system - there are different kinds of compilations. The bbfreeze will compile everything into multiple .so files, or copy the .egg files directly and they shall all be present in a single directory (or as a single file). There is no installation required. – Joe Jul 26 '16 at 8:16

Not exactly a packaging of the python code, but there is now also grumpy from google, which transpiles the code to Go. It doesn't support the python C api, so it may not work for all projects.

share|improve this answer

Use Cython to convert to c, compile and link with gcc. Another could be, make the core functions in c (the ones you want to make hard to reverse), compile them and use python boost to import the compiled code ( plus you get a much faster code execution). then use any tool mentioned to distribute.

share|improve this answer

I'm told that PyRun, https://www.egenix.com/products/python/PyRun/, is also an option.

share|improve this answer
1  
A link to a solution is welcome, but please ensure your answer is useful without it: add context around the link so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. Answers that are little more than a link may be deleted. – Petter Friberg Mar 28 at 18:50

protected by Josh Crozier Mar 5 at 3:12

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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