Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding packages to Python "embedded" installation for Windows

Tags:

python

embed

From the documentation:

The embedded distribution is a ZIP file containing a minimal Python environment.

Sounds great! The 64-bit Windows embedded v3.6.5 of Python is only 13MB. As an alternative to compiling, I would like to zip some python scripts together with the minimum needed to run them on a Win10 machine that doesn't have Python installed.

Now, I almost always need to import additional packages to provide functionality. But I can't determine how I should do this if I want to send out a python script together with this embedded version of Python. For example, if my script uses numpy, how can I include that library in this "embed?" I.e., so that on any Win10 machine I can unzip the single deployment file and immediately execute my scripts?

(A regular pip install numpy appears to create a Lib subdirectory that's over 50MB! But for an "embedded" deployment I don't need any support for debugging or whatever else is encompassed in that mass of files.)

like image 297
feetwet Avatar asked Apr 09 '18 16:04

feetwet


People also ask

What is the Python Windows embeddable package?

The embeddable package is a minimal package of Python suitable for embedding into a larger application.

What is difference between embeddable package and installer?

The executable installers provide the full Python Developer Kit; the embeddable package contains the runtime dependencies; nuget packages allow easy use of Python as a build tool; and site extensions for Azure App Service make it easier to manage Python as a web server dependency.


1 Answers

There is a way to extend Python embedded installation. I managed to create Flask-ready package, that I can just unzip on target machine and run code. The trick is to install EXACT same python version (normal full blown python) as your target embedded small python. Not only version but x86, x64 has to match as well.

Then install modules from pip on normal python, go to NormalPython\Lib\site-packages and copy all new files that appear after installing the package to EmbeddedPython\Lib finally add Lib and Lib\site-packages to pythonXX._pth inside Embedded python folder.

It's extremely important to fully test your application in case you miss some package. Also this would not work for packages that also add .exe to Scripts folder. You could still probably copy the exe's to Script folder and add it to path which could do the trick.

like image 163
Michał Rawluk Avatar answered Sep 18 '22 16:09

Michał Rawluk