Deploying application with Python or another embedded scripting language
Asked Answered
D

4

20

I'm thinking about using Python as an embedded scripting language in a hobby project written in C++. I would not like to depend on separately installed Python distribution. Python documentation seems to be quite clear about general usage, but I couldn't find a clear answer to this.

Is it feasible to deploy a Python interpreter + standard library with my application? Would some other language like Lua, Javascript (Spidermonkey), Ruby, etc. be better for this use?

Here's the criteria I'm weighing the different languages against:

  • No/Few dependencies on externally installed packages
  • Standard library with good feature set
  • Nice language :)
  • Doesn't result in a huge install package

edit:

I guess the question should be: How do I deploy my own python library + standard library with the installer of my program, so that it doesn't matter whether the platform already has python installed or not?

edit2:

One more clarification. I don't need info about specifics of linking C and Python code.

Digenesis answered 15/2, 2009 at 17:12 Comment(0)
H
20

Link your application to the python library (pythonXX.lib on Windows) and add the following to your main() function.

Py_NoSiteFlag = 1;  // Disable importing site.py
Py_Initialize();    // Create a python interpreter

Put the python standard library bits you need into a zip file (called pythonXX.zip) and place this and pythonXX.dll beside the executable you distribute. Have a look at PyZipFile in the the zipfile module.

Hawger answered 15/2, 2009 at 20:28 Comment(6)
It should also be noted that placing the python files in a zip file also carry performance implications. You get a smaller install size, but increased startup time. This is because it has to unzip and compile the files every time it starts up (it won't save a pyc file).Jadeite
Is it possible to just distribute .pyc files? What will happen if we have py files (not zipped) in a location that user only has read access to?Digenesis
Actually, if you create the archive using zipfile.PyZipFile it will only archive .pyc files. You can also create the archive by hand. Startup time is decreased because the zip header contains a list of all modules present and their binary offset.Hawger
can't find any official documentation on this, can you point me somewhere?Sabba
Years later, but my search lead me to this overview from microsoft which covered everything. Also worth mentioning that external packages could be pip installed to your own local install then copied into the pythonNN.zip file as if they were part of stdlib so they'll be available to CPython without any hassle when your app is distributed.Oyster
There is a good manual www6.software.ibm.com/developerworks/education/l-pythonscript/…She
E
9

The embedding process is fully documented : Embedding Python in Another Application. The documents suggests a few levels at which embedding is done, choose whatever best fits your requirements.

A simple demo of embedding Python can be found in the directory Demo/embed/ of the source distribution.

The demo is here, should be able to build from the distro.

  • Very High Level Embedding
  • Beyond Very High Level Embedding: An overview
  • Pure Embedding
  • Extending Embedded Python
  • Embedding Python in C++

From the standard library you can select the components that do not carry too much dependencies.

Entreat answered 15/2, 2009 at 17:28 Comment(1)
I think the question was unclear. I edited it a bit. Maybe it's more exact now.Digenesis
A
6

To extend the answer by gimel, there is nothing to stop you from shipping python.dll, using it, and setting a correct PYTHONPATH in order to use your own installation of the python standard library. They are just libraries and files, and your install process can just deal with them as such.

Arman answered 15/2, 2009 at 18:16 Comment(1)
Thanks for on-topic answer. :)Digenesis
S
0

If you're looking for a way to simply deploy a Python app, you could use Py2Exe. That would allow your app all the dependencies it needs without worrying what is or isn't already installed on the users machine.

[Edit] If Py2Exe is a no-go for embedded applications, could you simply get your installer to check for the relevent files and download / install anything that's missing?[/Edit]

[Edit2] Could Elmer be what you're looking for? [/Edit2]

Subset answered 15/2, 2009 at 17:42 Comment(1)
I'm not sure (I've never tried it myself). Py2exe just turns a python app including any dependencies it needs into a single executable.Subset

© 2022 - 2024 — McMap. All rights reserved.