How can I convert a .py to .exe for Python?
I'm trying to convert a fairly simple Python program to an executable and couldn't find what I was looking for, so I have a few questions (I'm running Python 3.6):
The methods of doing this that I have found so far are as follows
- downloading an old version of Python and using
pyinstaller/py2exe
- setting up a virtual environment in Python 3.6 that will allow me to do 1.
- downloading a Python to C++ converter and using that.
Here is what I've tried/what problems I've run into.
- I installed
pyinstaller
before the required download before it (pypi-something) so it did not work. After downloading the prerequisite file,pyinstaller
still does not recognize it. - If I'm setting up a virtualenv in Python 2.7, do I actually need to have Python 2.7 installed?
- similarly, the only python to C++ converters I see work only up until Python 3.5 - do I need to download and use this version if attempting this?
Steps to convert .py to .exe in Python 3.6
- Install Python 3.6.
- Install cx_Freeze, (open your command prompt and type
pip install cx_Freeze
. - Install idna, (open your command prompt and type
pip install idna
. - Write a
.py
program namedmyfirstprog.py
. - Create a new python file named
setup.py
on the current directory of your script. - In the
setup.py
file, copy the code below and save it. - With shift pressed right click on the same directory, so you are able to open a command prompt window.
- In the prompt, type
python setup.py build
- If your script is error free, then there will be no problem on creating application.
- Check the newly created folder
build
. It has another folder in it. Within that folder you can find your application. Run it. Make yourself happy.
See the original script in my blog.
setup.py:
from cx_Freeze import setup, Executable
base = None
executables = [Executable("myfirstprog.py", base=base)]
packages = ["idna"]
options = {
'build_exe': {
'packages':packages,
},
}
setup(
name = "<any name>",
options = options,
version = "<any number>",
description = '<any description>',
executables = executables
)
EDIT:
- be sure that instead of
myfirstprog.py
you should put your.py
extension file name as created in step 4; - you should include each
import
ed package in your.py
intopackages
list (ex:packages = ["idna", "os","sys"]
) -
any name, any number, any description
insetup.py
file should not remain the same, you should change it accordingly (ex:name = "<first_ever>", version = "0.11", description = ''
) - the
import
ed packages must be installed before you start step 8.
Python 3.6 is supported by PyInstaller.
Open a cmd window in your Python folder (open a command window and use cd
or while holding shift, right click it on Windows Explorer and choose 'Open command window here'). Then just enter
pip install pyinstaller
And that's it.
The simplest way to use it is by entering on your command prompt
pyinstaller file_name.py
For more details on how to use it, take a look at this question.
There is an open source project called auto-py-to-exe on GitHub. Actually it also just uses PyInstaller internally but since it is has a simple GUI that controls PyInstaller it may be a comfortable alternative. It can also output a standalone file in contrast to other solutions. They also provide a video showing how to set it up.
GUI:
Output:
Alternatively use pyinstaller directly:
pip install pyinstaller
pyinstaller filename
I can't tell you what's best, but a tool I have used with success in the past was cx_Freeze. They recently updated (on Jan. 7, '17) to version 5.0.1 and it supports Python 3.6.
Here's the pypi https://pypi.python.org/pypi/cx_Freeze
The documentation shows that there is more than one way to do it, depending on your needs. http://cx-freeze.readthedocs.io/en/latest/overview.html
I have not tried it out yet, so I'm going to point to a post where the simple way of doing it was discussed. Some things may or may not have changed though.
How do I use cx_freeze?