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

标签 python python-3.x compilation tkinter exe

如何使用模块 tkinter 和 ttk 将 python 3.4.3 脚本编译为自可执行 exe(独立)? (py2exe、pyinstaller、freeze 不起作用。)有什么建议吗?谢谢您

最佳答案

我所做的是

  1. 下载Portable Python
  2. 用其他语言创建一个可以编译为 exe 的文件
  3. 使该可执行文件使用我的 Python 文件调用可移植的 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;
}

您可以使用 devc++ 编译该文件.

评估

优点:

  • 另一种方法。

缺点:

  • 需要完整的可移植Python。
  • 不传递任何命令行参数。
  • 您可以使用 .bat 文件来完成此操作,它也可以工作。
  • 当前工作目录与调用者的工作目录不同。

关于python - 如何将python 3.4.3脚本编译为exe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31779453/

相关文章:

python - 使用 Python 向 Hive 表中插入多行

python - 在 Windows 上安装 PyAudio

python - urllib.urlretrieve 编码不保留

Python字典编辑词条

java - 二进制搜索程序将编译并运行但不会完成并且不会终止?

python - 使用多个 Python 版本构建 Boost

python - 如何将 pandas 数据帧(日期,值)拆分为两个单个数组 X=Date 和 y=Value?

python - 系统错误 : unknown opcode when loading model with Keras

python - Windows 上 Python 3 的基于 Web 的安装程序和可执行安装程序之间的区别

c - C 中的头文件和源文件如何工作?