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

前端 未结 6 1084
清酒与你
清酒与你 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 = "",
        options = options,
        version = "",
        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 = "", version = "0.11", description = '' )
    • the imported packages must be installed before you start step 8.

提交回复
热议问题