Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   A py2exe solution (http://www.programmingforums.org/showthread.php?t=7423)

Sane Dec 6th, 2005 4:54 PM

A py2exe solution
 
It hasn't been much of a problem, I'm guessing mainly because people have found a simple solution around it. But instead ColdDeath coded a solution for me.

The problem was when py2exe compiles it's programs, it outputs like so:

Your Directory
- build
- dist
- - program
- - - program.exe

Which means every client will need to open up "Your Directory/dist/program/program.exe". Not very convenient.

With this simple C++ program at: http://1v7.com/drsane/py2exe_solution.zip,
:

#include <iostream>
#include <fstream>
#include <stdlib.h>

using namespace std;

int main(int argc, char **argv)
{
        char direct_ini_contents[1024];        /* declare file string */
        char cwd[1024];                        /* declare cwd string */
        char fileName[1024];                    /* declare filename string */

        ifstream the_file ("redirect.ini");    /* open file */
        the_file >> direct_ini_contents;        /* send contents of file to string */
        the_file.close();                      /* close file object */

        cwd[0] = system("cd");                  /* set cwd to the cwd string */
        sprintf(fileName, "START %s%s",  cwd, direct_ini_contents);

        system(fileName);                      /* run file */

        return 0;
}

It reads the contents of "redirect.ini", appends it to the current working directory, and opens that file.

So in redirect.ini you can put "program/program.exe", then place the exe in the main folder. When the user opens this exe, it'll actually open up the exe located inside the redirect.ini.

Not sure if you need it as well, but if so, thank ColdDeath!

Arevos Dec 7th, 2005 6:37 AM

As I understand it, you only need the "dist/program" directory. The build directory contains temporary files related to the building process, and the root directory of your program contains the source, which isn't needed once the program is packaged up with py2exe.

My TreeDemoWin.zip program I designed some time ago is packaged with py2exe, yet fits neatly in a single directory.

Sane Dec 7th, 2005 3:01 PM

I see... so where's the source file? I thought Python needs the source file to run because it's open-sourced. =S

Arevos Dec 7th, 2005 3:25 PM

Python is open-source because the C source-code for the Python interpreter is open for anyone to read or adapt (if I recall, it has a BSD-like license). The reason that Python doesn't produce natively compiled programs is an another matter entirely; one that has technical, rather than legal, reasons.

Normally, Python takes a .py file and converts it into Python bytecode, which it then runs. In the case of imported modules, it caches this bytecode in .pyc files, so files don't need to be interpreted from scratch everytime they're run. As well as .py and .pyc files, there are .pyd files, which are actually just standard windows DLLs that have the Python C API, and .pyo files, which are standard Linux so libraries. There's probably something similar for Macs.

I don't know how Py2exe works, but I do know that for each .py file in my program, py2exe created a matching .pyd file. I suspect what occurs is that py2exe generates the pyc bytecode for each .py file, and then does some binary magic which embeds this into a .pyd.

Suffice to say, it works, and no source is required.

Sane Dec 7th, 2005 4:27 PM

Oh, that's not bad at all then! I always assumed because Python was "Open-Source", that meant it could only be run with the source files left unchanged. Cool.


All times are GMT -5. The time now is 5:40 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC