HACKER Q&A
📣 monroewalker

How to build a plug-in system with Python for a desktop app?


I'm building a desktop app with flutter and want to allow for plugins written in python. I'm planning on using protobuf to define the API and writing a utility package in Python that a plugin would use to define its behavior. The desktop app will run the plugin as a subprocess and communicate over the io pipes.

The main concern I have is with packaging the plugins and dealing with their dependencies. I want to avoid requiring anything more than python on a given machine in order to get the desktop app and plugins working. Should I bundle each plugins dependencies with the plugin? Or download dependencies as part of the installation of the plugin? Looking for general guidance on how to handle this or links to good articles on what's been done before


  👤 monroewalker Accepted Answer ✓
So far bundling the dependencies seems best since it's the easiest and I can live with the redundant space usage / extra size. Using poetry, `poetry build` will build a wheel for the plugin. Then `pip install path/to/wheel.whl --target /path/to/some/folder` to create a folder that has the plugin with all its dependencies. After zipping that and moving it to another machine, `PYTHONPATH=some/dir python3 some/dir/myapp/__init__.py` works to run the plugin.

https://stackoverflow.com/questions/26059111/build-a-wheel-e...


👤 ankitg12
Not an expert in Python plugins, but I asked chatgpt gpt-4 about this, and this is what it came up with - https://chat.openai.com/share/e1ad569f-4d4d-4057-8564-0d79a8... . See if this helps.

Essentially boils down to this:

Best Practices The correct approach to managing dependencies will depend on the specifics of your application and its plugins. However, here are some general best practices:

Document your dependencies: Make sure to provide clear documentation of all dependencies required for your plugin to function properly.

Specify dependency versions: When downloading dependencies during installation, always specify the version to ensure the plugin works as expected.

Regularly update your dependencies: Whether you bundle them with your plugin or download them during installation, make sure you regularly update your dependencies to patch any security vulnerabilities and benefit from new features or improvements.

Consider using virtual environments: To prevent conflicts between different plugins' dependencies, consider using Python's virtual environments. This isolates each plugin's dependencies in its own environment.

Leverage dependency management tools: Tools like Pipenv, Poetry, or conda can help you manage your dependencies effectively. These tools also support the creation of virtual environments.


👤 eternityforest
Why protobuf? Does it really need that or would JSON/MessagePack work?