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

前端 未结 6 1072
清酒与你
清酒与你 2020-11-22 00:55

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):

相关标签:
6条回答
  • 2020-11-22 00:56

    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.
    0 讨论(0)
  • 2020-11-22 00:58

    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:

    0 讨论(0)
  • 2020-11-22 01:02

    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?

    0 讨论(0)
  • 2020-11-22 01:14

    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.

    0 讨论(0)
  • 2020-11-22 01:21

    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>
    0 讨论(0)
  • 2020-11-22 01:21

    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

    0 讨论(0)
提交回复
热议问题