2

How to compile python 3.4.3 script with the module tkinter and ttk to an self-executable exe (standalone)? (py2exe, pyinstaller, freeze doesn't work.) any suggestions? Thank You

Poom1997
  • 47
  • 7
  • Possible duplicate of [How do I compile my Python 3 app to an .exe?](http://stackoverflow.com/questions/17907258/how-do-i-compile-my-python-3-app-to-an-exe) – Cees Timmerman May 18 '17 at 16:09

1 Answers1

1

What I do is

  1. download Portable Python
  2. create an file in an other language that can be compiled to exe
  3. make that executable call portable Python with my Python file.

Structure:

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

The C++ source code:

// 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;
}

You can compile the file with devc++.

Evaluation

Pros:

  • An other way to do it.

Cons:

  • Needs whole Portable Python.
  • No command line arguments are passed.
  • You can do it with a .bat file and it works, too.
  • The current working directory is different than the caller's one.
User
  • 14,131
  • 2
  • 40
  • 59