Thanks Owen, this is very helpful.

Wayne


From: Owen Kelly [mailto:oke...@biopeak.com]
Sent: Tuesday, October 7, 2014 12:14 PM
To: 'primary discussion list for use and development of cx_Freeze'
Subject: Re: [cx-freeze-users] Calling python from frozen exe

Hi all,

What this solution does is provide an available interpreter in the frozen app 
that is known to be identical to the interpreter that is running the app.
Here is some related code for when you want to actually use that interpreter. 
That last function for launching a python file and catching errors is not fully 
debugged, it works when it works, but there are a lot of corner cases that I 
did not test, YMMV. My goal was to use the included interpreter to run a python 
file called interactive_session.py; maybe your goal was just to provide a 
python prompt, in which case things simplify a bit I suppose.

def we_are_frozen():
    """Returns whether we are frozen via py2exe.
    This will affect how we find out where we are located."""
    import sys
    return hasattr(sys, "frozen")


def module_path():
    """ This will get us the program's directory,
    even if we are frozen using py2exe"""

    import sys
    import os.path

    if we_are_frozen():
        return os.path.dirname(unicode(sys.executable, 
sys.getfilesystemencoding( )))
    return os.path.dirname(unicode(str(__file__), sys.getfilesystemencoding( )))

def open_interactive_session(self):
                '''Open an external command shell running the 
interactive_session script.'''
                int_session_path = module_path() + os.path.sep + 
"interactive_session.py"
                python_cmd = sys.executable
                _platform = sys.platform
                if _platform in ["linux", "linux2"]:
                                # TODO: Note the call below stalls the main 
interface until user exits the interactive session.
                                interactive_session_command = 'xterm -T 
"MyApplication interactive" -e '+python_cmd+' '+int_session_path
                                exit_status = 
os.system(interactive_session_command)
                                # TODO: [linux] Is it better re safety to use 
Popen or check_call (below) instead of system (above)?
                                #       Likely, but current code for that is 
broken.
                                #try:
                                #    check_call(["xterm", "-T MyApplication 
interactive", "-e "+python_cmd+' '+int_session_path], shell=False)
                                #except CalledProcessError as e:
                                #    reply = QtGui.QMessageBox.question(self, 
'interactive session error',
                                #            repr(e), QtGui.QMessageBox.Ok ,  
QtGui.QMessageBox.Ok )
                elif _platform == "darwin":
                                pass # the call has not yet been tested for MAC
                elif _platform == "win32":
                                # Check that the python executable packaged 
with installation is functional.
                                cmd_start_folder = os.path.join(module_path(), 
'tools')
                                # During development, be sure to manually copy 
python.exe to myproject\tools\python.exe
                                # (but do not add python.exe to code control in 
that location, because it will break the
                                # app freezing process). With python.exe in 
that folder, the interactive session can
                                # be successfully launched from the development 
environment as well as from the installed app.
                                if not 
os.path.exists(os.path.join(cmd_start_folder,"python.exe")):
                                                reply = 
QtGui.QMessageBox.question(self, 'test for python.exe',
                                                                                
"failed to start python.exe\ninteractive session cannot run", 
QtGui.QMessageBox.Ok ,  QtGui.QMessageBox.Ok )
                                                return
                                # Run script using that locally installed python
                                try:
                                                check_call(["start", "/D", 
cmd_start_folder, "cmd",  "/c", ".\python.exe ..\interactive_session.py"], 
shell=True)
                                except CalledProcessError as e:
                                                reply = 
QtGui.QMessageBox.question(self, 'interactive session start error',
                                                                                
repr(e), QtGui.QMessageBox.Ok ,  QtGui.QMessageBox.Ok )
                                # end of Windows section

Regards,

Owen




From: Owen Kelly [mailto:oke...@biopeak.com]
Sent: October-07-14 1:55 PM
To: 'primary discussion list for use and development of cx_Freeze'
Subject: RE: [cx-freeze-users] Calling python from frozen exe

I have this code in my setup.py. I have cut out a lot, so this code won't 
actually work, but the parts that touch on copying python.exe are there.
The key idea is that you wait until you actually running cx_freeze to locate 
and copy the python.exe (or linux equivalent) into a known folder in the frozen 
app.
Here I am putting it in the tools folder.
Storing python.exe in a 'tools' folder is similar to including documentation in 
the frozen file, i.e. freeze only includes this stuff because you explicitly 
tell it to do so.

import sys
from cx_Freeze import setup, Executable
import os.path

# copy python.exe (pythonw.exe will not work) to myproject\tools\python.exe (or 
equivalent on linux)
# This approach causes the packaged python version to match what is running on 
the machine that builds the MSI file
python_executable_path = sys.executable
(python_exe_dir, python_exe_name) = os.path.split(python_executable_path)
python_executable_dest = os.path.join('tools', python_exe_name)

build_exe_options = {
    "packages": ["os", 'sys', 'myproject', 'bluetooth', 'myproject.ui', 
'myproject.common', 'scipy.signal',  'scipy.sparse', 'scipy.special',  
'scipy.integrate', 'pyqtgraph', 'send2trash'],
    "includes": ["PySide","sys"],
    "include_files": [(r"myproject\dfc_background.png", "dfc_background.png"),
        (r"myproject\docs","docs"),
        (r"myproject\tools","tools"),
        (python_executable_path, python_executable_dest),
        ]
    }

setup(
    # [ ... ]
    options = {"build_exe": build_exe_options},
    # [ ... ]
)

Hope this helps,

Owen

From: Wayne Boras [mailto:wayne.bo...@lethbridge.ca]
Sent: October-07-14 1:32 PM
To: 'primary discussion list for use and development of cx_Freeze'
Subject: Re: [cx-freeze-users] Calling python from frozen exe

I suppose that I could freeze the other py file and call its frozen exe, but 
I'm wondering if there is another way.

From: Wayne Boras [mailto:wayne.bo...@lethbridge.ca]
Sent: Tuesday, October 7, 2014 11:12 AM
To: 'cx-freeze-users@lists.sourceforge.net'
Subject: [cx-freeze-users] Calling python from frozen exe

Hello,

Is it possible to call the bundled python interpreter from within a frozen exe? 
I need to call a third-party py file which is designed to run from the command 
line, not natively using import statements.

Thanks,
Wayne

------------------------------------------------------------------------------
Meet PCI DSS 3.0 Compliance Requirements with EventLog Analyzer
Achieve PCI DSS 3.0 Compliant Status with Out-of-the-box PCI DSS Reports
Are you Audit-Ready for PCI DSS 3.0 Compliance? Download White paper
Comply to PCI DSS 3.0 Requirement 10 and 11.5 with EventLog Analyzer
http://pubads.g.doubleclick.net/gampad/clk?id=154622311&iu=/4140/ostg.clktrk
_______________________________________________
cx-freeze-users mailing list
cx-freeze-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/cx-freeze-users

Reply via email to