如何将python 3.4.3脚本编译为exe?

如何将python 3.4.3脚本编译为exe?,python,python-3.x,compilation,tkinter,exe,Python,Python 3.x,Compilation,Tkinter,Exe,如何使用模块tkinter和ttk将Python3.4.3脚本编译为自执行exe(独立)?(py2exe,pyinstaller,冻结不起作用。)有什么建议吗?谢谢我所做的是 下载 使用其他语言创建可编译为exe的文件 使用我的Python文件使可执行调用portable Python 结构: application_folder # the folder where everything is in +--my_python_folder # the folder where you

如何使用模块tkinter和ttk将Python3.4.3脚本编译为自执行exe(独立)?(py2exe,pyinstaller,冻结不起作用。)有什么建议吗?谢谢

我所做的是

  • 下载
  • 使用其他语言创建可编译为exe的文件
  • 使用我的Python文件使可执行调用portable Python
  • 结构:

    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;
    }
    
    //基于https://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx
    #包括
    #包括
    #包括
    int_tmain(int argc,TCHAR*argv[])
    {
    STARTUPINFO si;
    处理信息;
    //在pythonw.exe和python.exe之间进行选择
    TCHAR命令[]=“\”可移植Python 3\\App\\pythonw.exe\“\”my\u program.py\”;
    //启动Python程序的目录
    TCHAR目录[]=“我的python文件夹”;
    零内存(&si,sizeof(si));
    si.cb=sizeof(si);
    零内存(&pi,sizeof(pi));
    //启动子进程。
    如果(!CreateProcess(NULL,//没有模块名称(使用命令行)
    命令,//命令行
    NULL,//进程句柄不可继承
    NULL,//线程句柄不可继承
    FALSE,//将句柄继承设置为FALSE
    0,//没有创建标志
    NULL,//使用父级的环境块
    目录,//使用父目录的起始目录
    &si,//指向STARTUPINFO结构的指针
    &pi)//指向进程信息结构的指针
    ) 
    {
    printf(“CreateProcess失败(%d)。\n”,GetLastError());
    返回1;
    }
    /*
    //等待子进程退出。
    WaitForSingleObject(pi.hProcess,无限);
    //关闭进程和线程句柄。
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    */
    返回0;
    }
    
    您可以使用编译文件

    评价 优点:

    • 另一种方法
    缺点:

    • 需要整个可移植Python
    • 没有传递任何命令行参数
    • 你可以用一个.bat文件,它也可以工作
    • 当前工作目录与调用方的工作目录不同
    可能的副本