diff --git a/src/backend/InvenTree/plugin/apps.py b/src/backend/InvenTree/plugin/apps.py index 8c037ba114..fa7801d72e 100644 --- a/src/backend/InvenTree/plugin/apps.py +++ b/src/backend/InvenTree/plugin/apps.py @@ -5,11 +5,15 @@ The main code for plugin special sauce is in the plugin registry in `InvenTree/p """ import logging +import pathlib +import sys from django.apps import AppConfig +from django.conf import settings from maintenance_mode.core import set_maintenance_mode +from InvenTree.config import get_plugin_dir from InvenTree.ready import canAppAccessDatabase, isInMainThread, isInWorkerThread from plugin import registry @@ -23,7 +27,7 @@ class PluginAppConfig(AppConfig): def ready(self): """The ready method is extended to initialize plugins.""" - # skip loading if we run in a background thread + self.update_python_path() if not isInMainThread() and not isInWorkerThread(): return @@ -56,3 +60,29 @@ class PluginAppConfig(AppConfig): # drop out of maintenance # makes sure we did not have an error in reloading and maintenance is still active set_maintenance_mode(False) + + def update_python_path(self): + """Update the PYTHONPATH to include the plugin directory.""" + if not settings.PLUGINS_ENABLED: + # Custom plugins disabled - no need to update PYTHONPATH + logger.info('Custom plugins are not enabled') + return + + plugin_dir = get_plugin_dir() + + if not plugin_dir: + logger.warning('Plugins directory not specified') + return + + if plugin_dir := get_plugin_dir(): + path = pathlib.Path(plugin_dir) + + if not path.exists(): + try: + path.mkdir(parents=True, exist_ok=True) + except Exception: + logger.error(f'Failed to create plugin directory: {plugin_dir}') + return + + if plugin_dir not in sys.path: + sys.path.append(plugin_dir)