How to convert python .py file into an executable file for use cross platform?

57,909

Solution 1

There are two distinct ways of freezing python scripts to create executables:

  1. Packing the interpreter and *.pyc files into one exe file-container. Such an approach is used by tools like PyInstaller, Py2exe, cx_freeze.
  2. Creating native code from Python source, usually using a middle step of converting Python-source to C or C++ code. This is done by such tools as Shed-skin and Nuitka. The problem of this aproach is that such tools do not always support all the functionality of Python (e.g. they can have some typing limitations and so on)

The point where you have to start is reading the documentation. Such tools are not just push-and-run style tools, they usually have some configuration that must be implemented (that's the problem of possibly all build systems, and as the project grows, the configuration and number of hooks also grows).

You can start with Py2exe tutorial and 'hello-world' to get acquainted with that how compilation is done. As far as I know it's a simplest way to get your goal.

And the last thing, you can't create cross-platform native executables as their file formats are strongly operating system and hardware dependent.

Solution 2

  1. Download py2exe
  2. Download this msvcp90.dll
  3. Copy your FileCode.py AND msvcp90.dll to C:\Python27\
  4. In C:\Python27\ create new text file, then enter this code inside it:
from distutils.core import setup
import py2exe
setup(console=['Avril.py'])
  1. Replace Avril.py with YourFileName.py
  2. Save the file as setup.txt
  3. Open CMD and type this:

cd C:\Python27\
python setup.txt py2exe

  1. Now go to C:\Python27\dist\ and there's your .exe program.

Source: Manvir Singh

Solution 3

Python scripts can be made directly executable, like shell scripts, by putting the python environment path in the top of the script file.

#!/usr/bin/env python3.5 

The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.

Detailed description also for linux is here.

Solution 4

Install pyinstaller, a program that converts .py to .exe for python 2.7 to where python is located:

cd C:\python27\scripts
pip install pyinstaller

then move whatever python file you want to compile to C:\python27\scripts, compile from there by using:

pyinstaller --onefile yourfile.py

the --onefile is optional but it packages the whole thing(in this example yourfile.py) into one .exe. Once everything is done there will be 2 new folders along with a .spec file. From C:\python27\scripts open the folder dist. Your .exe will be located there in one file which you can double tap to execute and distribute to anyone who doesn't have python. Hope it helps.

Share:
57,909
Ricochet_Bunny
Author by

Ricochet_Bunny

Updated on October 10, 2020

Comments

  • Ricochet_Bunny
    Ricochet_Bunny over 3 years

    I've been searching through SO for a while now trying to come up with an answer to this but due to my inexperience with programming I don't understand much of the documentation, nor am I confident enough to experiment too much.

    Would anyone be able to describe in slightly simpler terms how I would use programs like Py2exe, PyInstaller, cx_freeze etc.? I just want a way for others (mainly friends) to be able to run my (simple, text only) program without having to download python themselves. If there is an easier way to do this I'd appreciate knowing that too.

    Running Vista 32bit, python 2.7

  • Ricochet_Bunny
    Ricochet_Bunny over 11 years
    I had already looked at documentation for all three of these, and tried to use the tutorial you mentioned already too - I haven't had any luck with the tutorial as what it is telling me should happen isn't, and I find the other documentation hard to understand. I apologise for being difficult but I was hoping someone would be able to provide an explanation/guide in easier terms.
  • Rostyslav Dzinko
    Rostyslav Dzinko over 11 years
    @Ricochet_Bunny, to my opinion py2exe tutorial is the easiest explanation to start with, you should possibly have to spend more time to catch it, nothing is simple).
  • Ricochet_Bunny
    Ricochet_Bunny over 11 years
    Ok, I'll give it another go. The only reason was that I wasn't getting the same results from doing the things it told me to and I don't know why.
  • Ricochet_Bunny
    Ricochet_Bunny over 11 years
    in a related matter, would it be possible to download just the Python IDLE on the user's computer and let it run on that? Or does the IDLE need the rest of the Python package to work?
  • Ricochet_Bunny
    Ricochet_Bunny over 11 years
    Also I assumed you could create cross platform executables because cx_freeze said it was capable of doing so.
  • Rostyslav Dzinko
    Rostyslav Dzinko over 11 years
    @Ricochet_Bunny, cx_freeze is able to create executables for different operating systems. But that doesn't mean you will get one executable. If you plan to distribute your application on OS X and Windows you must create two different distributions (app-bundle for Mac and exe for windows).
  • Ricochet_Bunny
    Ricochet_Bunny over 11 years
    Oh ok I understand.Any idea about the IDLE thing?
  • Thomas K
    Thomas K over 11 years
    @Ricochet_Bunny: Python comes with IDLE, so it's only one thing to download if you want to do it like that.
  • Ricochet_Bunny
    Ricochet_Bunny over 11 years
    I managed to figure out what was wrong with what I was doing - my cmd was not set to the directory the files were in, and I didn't know they needed to be. Thanks for your help.