如何编译python 3.4.3脚本到exe?

时间:2015-08-03 04:29:32

标签: python python-3.x compilation tkinter exe

如何使用模块tkinter和ttk将python 3.4.3脚本编译为自执行exe(独立)? (py2exe,pyinstaller,冻结不起作用。)任何建议?谢谢

1 个答案:

答案 0 :(得分:1)

我的工作是

  1. 下载Portable Python
  2. 使用可以编译为exe的其他语言创建文件
  3. 使用我的Python文件使可执行文件调用可移植Python。
  4. 结构:

    application_folder    # the folder where everything is in
    +--my_python_folder   # the folder where your python files are in 
    |  +--my_program.py   # the python file that you want to start
    +--Portable Python 3  # the Python version that you use
    +--program.exe        # the compiled program
    

    C ++源代码:

    // based on https://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx
    
    #include <windows.h>
    #include <stdio.h>
    #include <tchar.h>
    
    int _tmain( int argc, TCHAR *argv[] )
    {
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
        // choose between pythonw.exe and python.exe
        TCHAR command[] = "\"Portable Python 3\\App\\pythonw.exe\" \"my_program.py\"";
        // the directory where you start the Python program in
        TCHAR directory[] = "my_python_folder";
    
        ZeroMemory( &si, sizeof(si) );
        si.cb = sizeof(si);
        ZeroMemory( &pi, sizeof(pi) );
    
        // Start the child process. 
        if( !CreateProcess( NULL,   // No module name (use command line)
            command,        // Command line
            NULL,           // Process handle not inheritable
            NULL,           // Thread handle not inheritable
            FALSE,          // Set handle inheritance to FALSE
            0,              // No creation flags
            NULL,           // Use parent's environment block
            directory,           // Use parent's starting directory 
            &si,            // Pointer to STARTUPINFO structure
            &pi )           // Pointer to PROCESS_INFORMATION structure
        ) 
        {
            printf( "CreateProcess failed (%d).\n", GetLastError() );
            return 1;
        }
    /*
        // Wait until child process exits.
        WaitForSingleObject( pi.hProcess, INFINITE );
        // Close process and thread handles. 
        CloseHandle( pi.hProcess );
        CloseHandle( pi.hThread );
    */
        return 0;
    }
    

    您可以使用devc++编译文件。

    评价

    优点:

    • 另一种方法。

    缺点:

    • 需要整个便携式Python。
    • 没有传递命令行参数。
    • 您可以使用.bat文件进行操作,也可以。
    • 当前工作目录与来电者不同。