Questions on embedding Python

Fredrik Lundh fredrik at pythonware.com
Tue Nov 22 16:34:54 EST 2005


Kenneth McDonald wrote:

> 1) When Python is embedded, is it typically compiled statically into
> the program, or provided as a dynamic library?

if your application is compatible with the Python (i.e. uses /MD on
Windows, etc), using the DLL is a lot easier.

> 2) If provided as a dynamic library, does/should Python be recompiled
> into a tweaked dynamic library, or do we just use the python
> executable 'straight up', so to speak.

the DLL, yes.  if you're embedding, your application will take over
the role of python.exe.

> 3) We would of course want to make sure that 'our' Python (so to
> speak) would have access only to 'our' library of Python code (which
> would include most of the standard Python libraries), so that it
> can't be confused by the presence of some other version of Python on
> the system. Is this normally done by compiling in the Python search
> path, making it relative to the main application?

if you ship the Python DLL with your application, you can easily set
things up so it uses a Python library relative to the DLL (on Windows,
use GetModuleFileName to locate your application, extract the dir
name, and call Py_SetPythonHome to set PYTHONHOME to your
application directory.  Python will take care of the rest).

if you want to steal some code (windows-specific, but the relevant
portions shouldn't be too hard to port), the exemaker sources con-
tains a lot of stuff to search for DLL:s, set up paths, control loading
of the site module, etc.

    http://effbot.org/zone/exemaker.htm

(if you're really adventurous, turn your entire application into a
Python extension, and use an exemaker EXE to start it...)

> 4) I've never done this myself, but I understand that Python can
> import code from zip files in much the same way that Java can import
> from jar files. Is it possible to lump all of the python code we
> provide for use with the embedded Python into one big zip file, or
> would that cause problems such as slow loading when a module is
> imported? This would be nice because the fewer files we distribute,
> the better.

instead of adding directory "foo" to the path, zip it up, and add the
ZIP file to the path.  using ZIP files should be more efficient, since
Python caches the ZIP directory, and won't have to scan the disk
all the time.

</F> 






More information about the Python-list mailing list