How can I convert a .py to .exe for Python?

2018-12-31 20:03发布

问题:

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 Python3.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 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 2.7, do I actually need to have 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?

回答1:

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, 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.


回答2:

Python 3.6 still isn\'t supported by Pyinstaller. So in order to use it you\'re gonna need Python 3.5 or bellow. I\'m not sure about py2exe though.

Anyway, case 1 should be done like this:

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.

Update

Python 3.6 is now supported by Pyinstaller



回答3:

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

Output:

\"Output\"



回答4:

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

Docs show 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?



回答5:

py2exe is a distutils extension which allows to build standalone Windows executable programs (32-bit and 64-bit) from Python scripts; Python 3.3 and later are supported. It can build console executables, windows (GUI) executables, windows services, and DLL/EXE COM servers.

You can download it here:

https://pypi.org/project/py2exe/



回答6:

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.