186

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

  1. downloading an old version of Python and using pyinstaller/py2exe
  2. setting up a virtual environment in Python 3.6 that will allow me to do 1.
  3. 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?
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user7396807
  • 1,889
  • 3
  • 9
  • 5
  • 10
    Not sure why this is downvoted (perhaps because tool recommendations are offtopic for SO), but this question will be useful for others in the future. Similar previous questions have not been marked offtopic, e.g. http://stackoverflow.com/questions/2136837/process-to-convert-simple-python-script-into-windows-executable – Chris_Rands Jan 11 '17 at 14:54
  • 1
    Possible duplicate of [How to make a Python script standalone executable to run without ANY dependency?](https://stackoverflow.com/questions/5458048/how-to-make-a-python-script-standalone-executable-to-run-without-any-dependency) – Stevoisiak Feb 27 '18 at 18:54
  • You can also see the topic: [How to change .py to .exe](https://stackoverflow.com/questions/45004621/how-to-change-py-to-exe/65865848#65865848) – Milovan Tomašević Jan 24 '21 at 11:51

8 Answers8

154

Steps to convert .py to .exe in Python 3.6

  1. Install Python 3.6.
  2. Install cx_Freeze, (open your command prompt and type pip install cx_Freeze.
  3. Install idna, (open your command prompt and type pip install idna.
  4. Write a .py program named myfirstprog.py.
  5. Create a new python file named setup.py on the current directory of your script.
  6. In the setup.py file, copy the code below and save it.
  7. With shift pressed right click on the same directory, so you are able to open a command prompt window.
  8. In the prompt, type python setup.py build
  9. If your script is error free, then there will be no problem on creating application.
  10. 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 .pyextension file name as created in step 4;
  • you should include each imported package in your .py into packages list (ex: packages = ["idna", "os","sys"])
  • any name, any number, any description in setup.py file should not remain the same, you should change it accordingly (ex:name = "<first_ever>", version = "0.11", description = '' )
  • the imported packages must be installed before you start step 8.
Mefitico
  • 816
  • 1
  • 12
  • 41
Maria Irudaya Regilan J
  • 1,426
  • 1
  • 11
  • 22
  • 15
    Is there a way to make it ‘standalone’ executable? If I proceed as you advise in your article, I end up with folder exe.win.32-3.6. which is full of supportive files and directories (like ‘collections/’, ’email’, etc.). Awful a lot for a simple print(‘hello’). I would like to make one, single, executable file, that will run on another computer as is, without the need for other files. Is there a build option for that? – FanaticD Aug 31 '17 at 09:07
  • 1
    [cx_freeze ImportError: No module named 'idna'](https://stackoverflow.com/a/46967711/7032856) – Nae Oct 27 '17 at 04:51
  • 2
    @FanaticD Python is not your best bet. Cython (a nightmare to get working) or Nuitka would be your best bet. But getting those working is an achievement in itself. – Tetora Oct 30 '17 at 13:31
  • Would this work in Python 3.5.3? (The last stable version on Debian 9 Stretch). – Unix Aug 20 '18 at 22:03
  • How would I import `from sklearn.externals import joblib`? as mentioned in the edit, step 2. – Jack Aug 22 '18 at 01:31
  • My python program writes to a file. But when I execute the exe. It doesn't. Could you help me in this – Shabir A. Sep 23 '19 at 05:44
  • I did this with Python 3.8.1 and it worked. Kind of messy, lots of support libraries, maybe tweaking setup.py could reduce some of that? – Trashman Jan 31 '20 at 21:52
  • CX_Freeze can't work all the time. It attempts to install all dependencies and libraries in a folder, and then "creates" a pre-made .exe to run the setup. A better way I have used before is to copy my Python installation, have a little batch script to run the program, create a shortcut to run it, have a setup.py to install dependencies, and then I just package with IExpress, set the setup command to run setup.py, and, in the end, I have an installer that works just fine. – Dukk Feb 24 '21 at 20:32
  • @ShabirA. Use `pyinstaller/py2exe` it worked for me, my program writes to files and it works. – ChinDaMay Jan 17 '22 at 22:32
111

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rodrigo Nascimento
  • 1,594
  • 3
  • 10
  • 14
  • 6
    just tested with python 3.7.0. A lot of warnings though but compiled successfully and running on windows 10 – Ole_S Jan 21 '19 at 07:14
  • 2
    I compiled a program with python 3.5.0 and no warnings or errors, and it runs as expected – Menotdan Oct 12 '19 at 14:25
  • 1
    Does anybody know what matters in a Pyinstaller? Will a exe build on windows 10 work in windows 8 ? or will windows 10 32bit build work in windows 10 64 bit systems? –  Jun 06 '20 at 11:21
  • @Peter I encountered the following error: AttributeError: Module 'PyQt5' has no attribute '__version__' – Yam Jul 22 '21 at 05:55
  • runs and creates exe like charm, but when it comes to distribution, antivirus causes tons of troubles. Now I am pulling my hair to find an alternate to stop those antivirus warning just for a hello world code – Ratul Hasan Aug 18 '21 at 14:25
  • "The 'typing' package is an obsolete backport of a standard library package and is incompatible with PyInstaller. Please `pip uninstall typing` then try again." why this error is happening? – Muhammad Ali Feb 26 '22 at 05:50
38

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:

Auto Py to Exe

Output:

Output

Alternatively use pyinstaller directly:

pip install pyinstaller
pyinstaller filename
Gab
  • 512
  • 4
  • 7
  • 4
    You should have mentioned that it requires MVC++. error: Microsoft Visual C++ 14.0 is required. – SKR Nov 28 '18 at 00:29
  • 4
    I don't find any value in this since it is built on pyinstaller and if you get an error in pyinstaller you are going to have it here too! – CV_passionate Apr 30 '19 at 18:19
13

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gerschel
  • 131
  • 2
  • 6
10

Now you can convert it by using PyInstaller. It works with even Python 3.

Steps:

  1. Fire up your PC
  2. Open command prompt
  3. Enter command pip install pyinstaller
  4. When it is installed, use the command 'cd' to go to the working directory.
  5. Run command pyinstaller <filename>
LoneWolf
  • 339
  • 4
  • 17
  • 4
    because it doesn't work on Python 3.6 which OP specifically wrote he's using. – uri Apr 10 '19 at 14:40
  • 2
    Pyinstaller should work now. If it doesn't, then tell me in the comments – LoneWolf Jun 05 '20 at 15:51
  • 2
    In step 5, instead of pyinstall it should be pyinstaller . – Ayelet Zadock Sep 29 '20 at 10:04
  • 1
    My main python file has a dependency to a custom python file. How to include my custom.py while creating the exe ? – roney Oct 01 '20 at 16:12
  • @roney Sorry for the ultra late reply but I got locked out of my SO account but the answer is here. Or, more precisely, here: https://pyinstaller.readthedocs.io/en/stable/spec-files.html. It's just too big to write in a comment. – LoneWolf Nov 21 '20 at 16:38
8

I've been using Nuitka and PyInstaller with my package, PySimpleGUI.

Nuitka There were issues getting tkinter to compile with Nuikta. One of the project contributors developed a script that fixed the problem.

If you're not using tkinter it may "just work" for you. If you are using tkinter say so and I'll try to get the script and instructions published.

PyInstaller I'm running 3.6 and PyInstaller is working great! The command I use to create my exe file is:

pyinstaller -wF myfile.py

The -wF will create a single EXE file. Because all of my programs have a GUI and I do not want to command window to show, the -w option will hide the command window.

This is as close to getting what looks like a Winforms program to run that was written in Python.

[Update 20-Jul-2019]

There is PySimpleGUI GUI based solution that uses PyInstaller. It uses PySimpleGUI. It's called pysimplegui-exemaker and can be pip installed.

pip install PySimpleGUI-exemaker

To run it after installing:

python -m pysimplegui-exemaker.pysimplegui-exemaker

Mike from PSG
  • 5,312
  • 21
  • 39
5

The best and easiest way is auto-py-to-exe for sure, and I have given all the steps and red flags below which will take you just 5 mins to get a final .exe file as you don't have to learn anything to use it.

1.) It may not work for python 3.9 on some devices I guess.

2.) While installing python, if you had selected 'add python 3.x to path', open command prompt from start menu and you will have to type pip install auto-py-to-exe to install it. You will have to press enter on command prompt to get the result of the line that you are typing.

3.) Once it is installed, on command prompt itself, you can simply type just auto-py-to-exe to open it. It will open a new window. It may take up to a minute the first time. Also, closing command prompt will close auto-py-to-exe also so don't close it till you have your .exe file ready.

4.) There will be buttons for everything you need to make a .exe file and the screenshot of it is shared below. Also, for the icon, you need a .ico file instead of an image so to convert it, you can use https://convertio.co/

5.) If your script uses external files, you can add them through auto-py-to-exe and in the script, you will have to do some changes to their path. First, you have to write import sys if not written already, second, you have to make a variable for eg, location=getattr(sys,"_MEIPASS",".")+"/", third, the location of example.png would be location+"/example.png" if it is not in any folder.

6.) If it is showing any error, it may probably be because of a module called setuptools not being at the latest version. To upgrade it to the latest version, on command prompt, you will have to write pip install --upgrade setuptools. Also, in the script, writing import setuptools may help. If the version of setuptools is more than 50.0.0 then everything should be fine.

7.) After all these steps, in auto-py-to-exe, when the conversion is complete, the .exe file will be in the folder that you would have chosen (by default, it is 'c:/users/name/output') or it would have been removed by your antivirus if you have one. Every antivirus has different methods to restore a file so just experiment if you don't know.

Here is how the simple GUI of auto-py-to-exe can be used to make a .exe file.

enter image description here

Mudit Bhatia
  • 91
  • 1
  • 4
  • 1
    I am sorry if I answered this question late. I did not know about python or this site when this question was asked. However, this method is very easy and quick and it works for me every time and you don't need any knowledge to use it. – Mudit Bhatia Nov 28 '20 at 07:31
2

PyOxidizer can be an option here. It's pretty popular with 3.3k stars on Github. Its documentation says

PyOxidizer is capable of producing a single file executable - with a copy of Python and all its dependencies statically linked and all resources (like .pyc files) embedded in the executable. You can copy a single executable file to another machine and run a Python application contained within. It just works.

While I'm not sure if it is capable of producing .exe file PyOxidizer definitely helps with packaging and distribution.

gman
  • 116
  • 1
  • 4