Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get python embedded to work with zip'd library

I'm trying to embed python, and provide the dll and a zip of the python libraries and not use any installed python. That is, if a user doesn't have python, I want my code to work using the provided dll/zip.

This post seems to describe the process, except it isn't working for me.

If I run the following, my code will run as long as I have Python27.dll and a folder named Python27 that contains the DLL and Lib folders.

Py_SetProgramName(argv[0]);  /* optional but recommended */
Py_SetPythonHome("Python27");
Py_Initialize();

If I remove the Python27 folder, the code fails - so I am pulling in the local copy, not any installed python.

However, if I zip the local Python27 folder, the code stops running, giving "ImportError: No module named site".

PEP273 makes it sound like this should just work, but everything I've tried has failed.

Can anyone shed light on how to get embedded python to run from a zip file?

Given that there are related questions that have gone unanswered, I think it would be helpful if people would add a comment if they have successfully gotten reading from a zip file working, even if they aren't sure what I might need to fix.

That would at least help me understand if I should keep looking for an answer!

Update: No matter what I try (even with LoadLibrary as suggested), I can run my program from a fully unzipped directory. Any time I remove the directory with DLLs/* and Lib/* and put in Python27.zip instead, I just get

ImportError: No module named site
like image 206
Brett Stottlemyer Avatar asked Aug 25 '13 11:08

Brett Stottlemyer


People also ask

What is embedded zip Python?

The embeddable package. New in version 3.5. The embedded distribution is a ZIP file containing a minimal Python environment. It is intended for acting as part of another application, rather than being directly accessed by end-users.

What is embedded Python?

Embedded Python is an extension of the Python programming language that allows for execution of Python code inside the InterSystems IRIS process context. Because Embedded Python shares the same process context as ObjectScript, it can interact natively with objects written in ObjectScript.


1 Answers

I had two issues.

The not well documented 'Py_NoSiteFlag' fixed the

ImportError: No module named site

first problem. Next, I had to update paths. In the end, I ended up with the following for initialization:

Py_NoSiteFlag=1;
Py_SetProgramName(argv[0]);
Py_SetPythonHome(".");
Py_InitializeEx(0);
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path = ['.','python27.zip','python27.zip/DLLs','python27.zip/Lib','python27.zip/site-packages']");

[edit to address question in comments] Once you have the zip'd file loading, you still need to pass files to the python engine. Here's what I use (argument and file existence checks not included).

PyObject* PyFileObject = PyFile_FromString(argv[1], "r");
int res = PyRun_SimpleFile(PyFile_AsFile(PyFileObject), argv[1]);

[/edit]

like image 142
Brett Stottlemyer Avatar answered Sep 27 '22 18:09

Brett Stottlemyer