David Cortesi píše v Čt 11. 04. 2013 v 15:26 -0700: > The doc especially needs help from anyone who has actual > experience with:
One area that is not described well is the process how the frozen app get's executed. The section 'iu.py: An imputil Replacement' contain some information related to that. But this section should be dropped since it is completely outdated. There could be a section like: 'Bootstrap process' or similar: Bootstrap Process ================= There are many steps that have to be done before actually executing the main user's code. This process "doing all the necessary steps" is called bootstrapping. In the frozen app this could be divided into two main phases: 1. running the bootloader 2. running the python code Bootloader ---------- Bootloader prepares everything for running Python code. Bootloader will start do any setup and rerun itself in another process. This approach (mode with two processes) allows a lot of flexibility. So do not be surprised if you will see two processes of your frozen app in your system task manager. The mode with two process is not used only for onedir mode on Windows. This is the only exception. What happens during execution of bootloader: A. 1st process - bootloader starts 1. extract bundled files to temppath/_MEI123456 - in onefile mode 2. set/unset various environment variables: - e.g. override LD_LIBRARY_PATH on Linux, LIBPATH on AIX. - unset DYLD_LIBRARY_PATH on OSX, 3. set handling signals of the process: e.g. when the child prosess is not terminated correctly - redirect signals to the parent process or some signals are ignored. 4. run the child process 5. wait for the child process to finish 6. remove extracted files from temppath/_MEI123456 - in onefile mode B. 2nd process - bootloader started itself as a child process. 1. on Windows set activation context - for more info: http://msdn.microsoft.com/en-us/library/windows/desktop/aa374153(v=vs.85).aspx 2. Load Python dynamic library. The name of the dynamic library is embedded in the .exe file. 3. Initialize python interpreter - set PYTHONPATH, PYTHONHOME, 4. Run python code. Running python code ------------------- Running Python code consists of several steps: 1. Run python initialization code - the very first initialization code uses - it prepares everything for running the main user's script. - The initialization code can use only the python builit-in modules because during initialization other python modules are not yet available for import. - instructs the python import machinery to be able to load modules from the .exe file. 2. Run run-time hooks. 3. Install python .egg files. - when some modules are part of an zip file (.egg), pyinstlaller will bundle this egg into the ./eggs directory. - installing means appending .egg file names to sys.path: sys.path == ['_MEIPASS/eggs/file1.egg', '_MEIPASS/eggs/file2.egg'] - python automatically detect if item in sys.path is zip file or a directory. 4. Run the main script. - and now continues the user's code Python import mechanism in frozen app ------------------------------------- PyInstaller embeds compiled python code within the .exe file. Thus there has to be some code that injects the python import machinery. Python implements support for injecting this import mechanism. This mechanism is described in PEP 302 -- New Import Hooks (http://www.python.org/dev/peps/pep-0302/). PyInstaller implements the PEP 302 specification for built-in modules, frozen modules (compiled python code within the frozen app) and for C-extensions. Code for that can be found in ./PyInstaller/loader/pyi_importers.py. At run-time of the frozen app the PyInstaller PEP 302 hooks are appended to variable sys.meta_path. Modifying This variable causes Python interpreter to load the frozen python modules from the .exe file. When trying to import python modules in the code Python interpreter will first try PEP 302 hooks in sys.meta_path before trying searching paths in sys.path. In the frozen app this is the resolution order of import statements when looking for python modules: 1. Is built-in module? List of built-in modules is in variable sys.builtin_module_names. If not then try other type. 2. Is frozen module (module from the .exe file)? Then load it from the .exe file or try other type. 3. C-extension? The frozen app will try to find a file with name 'package.subpackage.module.pyd' or 'package.subpackage.module.so' If there is not a file with that name then continue. 4. Now examine paths in the sys.path (PYTHONPATH). Python in the frozen app will try load module from that paths. As paths there could be any additional location with python modules or .egg filenames from the './eggs' where pyinstaller bundles .egg files. 5. If the module is not found then 'ImportError' exception is raised. -- You received this message because you are subscribed to the Google Groups "PyInstaller" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/pyinstaller?hl=en. For more options, visit https://groups.google.com/groups/opt_out.
