Unable to get python embedded to work with zipd library

2024/11/16 19:55:42

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
Answer

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]

https://en.xdnf.cn/q/71303.html

Related Q&A

Convert integer to a random but deterministically repeatable choice

How do I convert an unsigned integer (representing a user ID) to a random looking but actually a deterministically repeatable choice? The choice must be selected with equal probability (irrespective o…

Using python opencv to load image from zip

I am able to successfully load an image from a zip:with zipfile.ZipFile(test.zip, r) as zfile:data = zfile.read(test.jpg)# how to open this using imread or imdecode?The question is: how can I open thi…

DeprecationWarning: Function when moving app (removed titlebar) - PySide6

I get when I move the App this Warning: C:\Qt\Login_Test\main.py:48: DeprecationWarning: Function: globalPos() const is marked as deprecated, please check the documentation for more information.self.dr…

Architecture solution for Python Web application

Were setting up a Python REST web application. Right now, were using WSGI, but we might do some changes to that in the future (using Twisted, for example, to improve on scalability or some other featu…

Capture image for processing

Im using Python with PIL and SciPy. i want to capture an image from a webcam then process it further using numpy and Scipy. Can somebody please help me out with the code.Here is the code there is a pre…

Loading Magnet LINK using Rasterbar libtorrent in Python

How would one load a Magnet link via rasterbar libtorrent python binding?

Python currying with any number of variables

I am trying to use currying to make a simple functional add in Python. I found this curry decorator here.def curry(func): def curried(*args, **kwargs):if len(args) + len(kwargs) >= func.__code__…

Python - Display rows with repeated values in csv files

I have a .csv file with several columns, one of them filled with random numbers and I want to find duplicated values there. In case there are - strange case, but its what I want to check after all -, I…

Defining __getattr__ and __getitem__ on a function has no effect

Disclaimer This is just an exercise in meta-programming, it has no practical purpose.Ive assigned __getitem__ and __getattr__ methods on a function object, but there is no effect...def foo():print &quo…

thread._local object has no attribute

I was trying to change the logging format by adding a context filter. My Format is like thisFORMAT = "%(asctime)s %(VAL)s %(message)s"This is the class I use to set the VAL in the format. cla…