Install Python plugin dependencies with uv

GA
19d ago
+4170-100

Python plugins have to ship their libraries in a lib folder right now. The author runs pip install -t lib in CI, and whatever lands there is built for the author's Python. Anything with compiled parts like pydantic-core, lxml or numpy breaks as soon as a user runs a different Python version or architecture, and the zips get big.

This lets a plugin list its libraries in plugin.json. Flow installs them on the user's machine with uv, for the Python that will actually run the plugin. Flow's own Python now comes from uv too.

"Dependencies": ["requests==2.32.3", "pywin32==306; sys_platform == \"win32\""]

Python runtime

Flow no longer downloads the embeddable Python 3.11.4 through Droplex. When it needs a Python it runs uv python install for a version pinned in the code, currently 3.11.17. That's still 3.11, so plugins that bundle compiled 3.11 libraries in lib keep working, and it picks up about two years of 3.11 security fixes. Bumping it later is a one-line change in UvPythonRuntime, next to the uv version pin.

  • It's a full CPython build, not the embeddable one, so there's no ._pth file and things like tkinter and venv are there. The embeddable never loaded the user site-packages, so plugins on Flow's runtime get PYTHONNOUSERSITE=1 and a pip install --user package still can't shadow a plugin's lib.
  • It's the x64 build, and the x86 build on 32-bit Windows. ARM64 also gets x64 under emulation, like the old embeddable, since plugins bundle x64 wheels in lib and a lot of packages still have no win_arm64 wheels.
  • It installs into Environments/Python/Runtime with --no-bin --no-registry, so it doesn't add anything to PATH or the Windows registry, and uv config files are ignored.
  • The first-run prompt is the same. You can still pick your own pythonw.exe and Flow leaves it alone.
  • People on the old embedded 3.11.4 get moved over automatically on their first start after updating. The new runtime is installed first and the setting only changes once that worked, so an offline start keeps the old Python until the next try. The old folder is removed on a later start, after the new path has been saved. Their plugin dependencies reinstall once since the Python changed.
  • Every Python plugin now needs uv because of this, not only plugins with Dependencies. Node and the Everything download still use Droplex.

Dependencies

  • Entries have to be pinned to an exact version. A plugin with an unpinned entry fails to load and the error names the entry. For plugin development there's a new "Allow unpinned Python plugin dependencies" toggle in Settings > General.
  • Flow uses the uv from the new uv path setting, then PATH. If neither has one, it downloads uv 0.13.7 (x64, ARM64 or x86) into Environments/uv and checks it against a SHA-256 kept in the code. When Flow moves to a newer pinned uv, the old one stays until the new one is downloaded. uv gets the same proxy as Flow's own downloads through HTTP_PROXY/HTTPS_PROXY.
  • Each plugin gets its own folder under Environments/Python/PluginDependencies/<plugin id>/, so one plugin's install can't change another plugin's libraries. Every install attempt writes to a new folder and install.json records the one in use. If nothing changed since the last start, uv isn't called at all, so offline starts keep working.
  • Versions only change when the plugin's Dependencies, the Python path or the Python version change. There's no --upgrade.
  • The install runs in the plugin's InitAsync before its process starts. It also runs in the background right after a store install if Python and uv are already set up.
  • The dependency folder goes on sys.path through site.addsitedir, which keeps pywin32's .pth files working. It's placed ahead of the interpreter's site-packages. Otherwise a package installed globally in a user's own Python beats the pinned one. Existing lib folders still work like before.
  • A failed install doesn't disable the plugin. Flow shows one error with the tail of uv's output, logs the rest, keeps the last good install and tries again on the next start. The plugin gets no queries for the rest of that session. Plugins the user has disabled don't get the toast.
  • Uninstalling removes the plugin's dependency folder and cancels an install still running for it. If a file was still in use and the folder couldn't be deleted, it's removed on a later start. Updates keep it, unless the new version has no Dependencies left. Old install folders get cleaned up on the next start instead of right away, because a running V2 plugin still has those files open.
  • Paths in the Python launch snippet are escaped now instead of using raw strings. That also fixes plugins whose path has an apostrophe in it, like a C:\Users\O'Brien profile.

PluginsLoader.Plugins is internal now because it takes the internal installer.

Testing

Added tests under Flow.Launcher.Test/PythonDependencies for requirement pinning, the install marker, the uv locator, the dependency installer and the runtime installer with a fake process runner, the process runner's cancel path, the switch from the old runtime, and the launch arguments.

So far I've only built this on Linux, and I ran the new tests in a separate net9.0 project that compiles the same source and test files. I haven't run it on Windows yet. Things I still want to check there:

  • a fresh install of the 3.11.17 runtime, and the switch from an existing 3.11.4 install, online and offline
  • pywin32 and a native wheel like pydantic-core
  • an offline start after a successful dependency install, and a first install while offline
  • a user Python with a conflicting global package