This article was originally published on Just4Fun

origin

The embedded Python environment discussed here is not the embedded one for hardware embedded development. If you are interested in Python and hardware, microPython is recommended.

Always wanted an embedded Python environment, preferably with changes to the environment (such as adding third-party packages) that could be re-distributed to users.

Embedded Python interpreters are often found in software that supports customization, such as GIMP, Blender, Sublime Text, Thonny…

I’ve always been interested in these projects, but I’ve never really understood how they work. I thought I was using some arcane packaging technique.

Yesterday, @Bi submitted a Codelab-Adapter Jupyter plug-in that allows the codelab-Adapter GUI to be used to start and stop Jupyter, which is installed in an embedded Python environment (actually a ZIP package).

I am very excited that Mr. Bi has fully achieved our previous goals. With this job, THERE is no doubt that codelab-Adapter has opened a new door. All of a sudden, the breadth of what we can do next is infinite. If we had more control over the environment, it would be less painful for entry-level users. Mr. Bee mentioned building an easy-to-use environment. I like a quote from Alan Kay:

Codelab – pain points of adapter

Codelab-adapter has always wanted a flexible and controllable Python environment, preferably allowing users to install third-party libraries. Since we used Pyinstaller for cross-platform packaging (freezing the entire project), the packaged environment could not support PIP.

For our current strategy of introducing third-party Python libraries, we prefer to use ZeroMQ to bridge the system environment.

Although Thonny showed great packing skills, it was done almost manually, and we didn’t like it.

Teacher Bee’s work shows how much easier it is to use an embedded Python environment directly.

Embedded Python environment

The official Python community provides a usable embedded Python environment that I hadn’t noticed before.

Currently, only Python on Windows is provided. (Python comes with Mac and Linux!)

The official Python community provides the environment for CPython, of course, and I know of projects that use the embedded environment provided by PyPy.

Using the notes

Write down some of my usage notes.

Download the embedded Python environment

I download the latest version 3.7.3: python – 3.7.3 – embed – win32. Zip, md5 is 70 df01e7b0c1b7042aabb5a3c1e2fbd5

I chose a 32-bit system and recommend that you also use the 32-bit version, which will run on a 64-bit system and not vice versa.

Install the PIP

Download get-pip.py to the unzipped directory:

Then run :.\python get-pip.py

Modify python37. _pth:

python37.zip
.

# Uncomment to run site.main() automatically
import site
Copy the code

Then you can use PIP to install a third-party library, we try to install jupyterlab: \ python -m PIP install jupyterlab -i https://mirrors.aliyun.com/pypi/simple

There is an error: PyRSistent installation error. Manually install PyRSistent: www.lfd.uci.edu/~gohlke/pyt…

.\python -m pip install pyrsistent‑0.15.1‑cp37‑cp37m‑win32.whl

Enable Jupyterlab :.\python -m jupyterlab

Then distribute the entire directory!

Be more professional

Giving the ZIP package directly to the user may seem unprofessional, but you can use Pynsist to package it as an installable file.