We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Failing to load sqlalchemy-plugin in pyinstaller'ed exe
This line of code works perfectly fine while running as a file from spyder.

engine =  sqlalchemy.create_engine('teradata://uid:pwd@DBinstance?driver=Teradata')
However, after making an exe file using pyinstaller, I get the following error while running the exe:

sqlalchemy.exc.NoSuchModuleError: Can't load plugin:sqlalchemy.dialects:teradata

This looks to be an exe-creation-issue in loading libraries. Has anyone encountered a similar issue and was able to resolve? Any pointers would be appreciated.

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `PyInstaller/hooks/hook-sqlalchemy.py`
Content:
```
1 #-----------------------------------------------------------------------------
2 # Copyright (c) 2005-2023, PyInstaller Development Team.
3 #
4 # Distributed under the terms of the GNU General Public License (version 2
5 # or later) with exception for distributing the bootloader.
6 #
7 # The full license is in the file COPYING.txt, distributed with this software.
8 #
9 # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
10 #-----------------------------------------------------------------------------
11 
12 import re
13 import importlib.util
14 
15 from PyInstaller import isolated
16 from PyInstaller.lib.modulegraph.modulegraph import SourceModule
17 from PyInstaller.utils.hooks import check_requirement, logger
18 
19 # 'sqlalchemy.testing' causes bundling a lot of unnecessary modules.
20 excludedimports = ['sqlalchemy.testing']
21 
22 # Include most common database bindings some database bindings are detected and include some are not. We should
23 # explicitly include database backends.
24 hiddenimports = ['pysqlite2', 'MySQLdb', 'psycopg2', 'sqlalchemy.ext.baked']
25 
26 if check_requirement('sqlalchemy >= 1.4'):
27     hiddenimports.append("sqlalchemy.sql.default_comparator")
28 
29 
30 @isolated.decorate
31 def _get_dialect_modules(module_name):
32     import importlib
33     module = importlib.import_module(module_name)
34     return [f"{module_name}.{submodule_name}" for submodule_name in module.__all__]
35 
36 
37 # In SQLAlchemy >= 0.6, the "sqlalchemy.dialects" package provides dialects.
38 # In SQLAlchemy <= 0.5, the "sqlalchemy.databases" package provides dialects.
39 if check_requirement('sqlalchemy >= 0.6'):
40     hiddenimports += _get_dialect_modules("sqlalchemy.dialects")
41 else:
42     hiddenimports += _get_dialect_modules("sqlalchemy.databases")
43 
44 
45 def hook(hook_api):
46     """
47     SQLAlchemy 0.9 introduced the decorator 'util.dependencies'.  This decorator does imports. E.g.:
48 
49             @util.dependencies("sqlalchemy.sql.schema")
50 
51     This hook scans for included SQLAlchemy modules and then scans those modules for any util.dependencies and marks
52     those modules as hidden imports.
53     """
54 
55     if not check_requirement('sqlalchemy >= 0.9'):
56         return
57 
58     # this parser is very simplistic but seems to catch all cases as of V1.1
59     depend_regex = re.compile(r'@util.dependencies\([\'"](.*?)[\'"]\)')
60 
61     hidden_imports_set = set()
62     known_imports = set()
63     for node in hook_api.module_graph.iter_graph(start=hook_api.module):
64         if isinstance(node, SourceModule) and node.identifier.startswith('sqlalchemy.'):
65             known_imports.add(node.identifier)
66 
67             # Read the source...
68             with open(node.filename, 'rb') as f:
69                 source_code = f.read()
70             source_code = importlib.util.decode_source(source_code)
71 
72             # ... and scan it
73             for match in depend_regex.findall(source_code):
74                 hidden_imports_set.add(match)
75 
76     hidden_imports_set -= known_imports
77     if len(hidden_imports_set):
78         logger.info("  Found %d sqlalchemy hidden imports", len(hidden_imports_set))
79         hook_api.add_imports(*list(hidden_imports_set))
80
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/PyInstaller/hooks/hook-sqlalchemy.py b/PyInstaller/hooks/hook-sqlalchemy.py
--- a/PyInstaller/hooks/hook-sqlalchemy.py
+++ b/PyInstaller/hooks/hook-sqlalchemy.py
@@ -14,7 +14,9 @@
 
 from PyInstaller import isolated
 from PyInstaller.lib.modulegraph.modulegraph import SourceModule
-from PyInstaller.utils.hooks import check_requirement, logger
+from PyInstaller.utils.hooks import check_requirement, collect_entry_point, logger
+
+datas = []
 
 # 'sqlalchemy.testing' causes bundling a lot of unnecessary modules.
 excludedimports = ['sqlalchemy.testing']
@@ -41,6 +43,13 @@
 else:
     hiddenimports += _get_dialect_modules("sqlalchemy.databases")
 
+# Collect additional dialects and plugins that are registered via entry-points, under assumption that they are available
+# in the build environment for a reason (i.e., they are used).
+for entry_point_name in ('sqlalchemy.dialects', 'sqlalchemy.plugins'):
+    ep_datas, ep_hiddenimports = collect_entry_point(entry_point_name)
+    datas += ep_datas
+    hiddenimports += ep_hiddenimports
+
 
 def hook(hook_api):
     """

{"golden_diff": "diff --git a/PyInstaller/hooks/hook-sqlalchemy.py b/PyInstaller/hooks/hook-sqlalchemy.py\n--- a/PyInstaller/hooks/hook-sqlalchemy.py\n+++ b/PyInstaller/hooks/hook-sqlalchemy.py\n@@ -14,7 +14,9 @@\n \n from PyInstaller import isolated\n from PyInstaller.lib.modulegraph.modulegraph import SourceModule\n-from PyInstaller.utils.hooks import check_requirement, logger\n+from PyInstaller.utils.hooks import check_requirement, collect_entry_point, logger\n+\n+datas = []\n \n # 'sqlalchemy.testing' causes bundling a lot of unnecessary modules.\n excludedimports = ['sqlalchemy.testing']\n@@ -41,6 +43,13 @@\n else:\n     hiddenimports += _get_dialect_modules(\"sqlalchemy.databases\")\n \n+# Collect additional dialects and plugins that are registered via entry-points, under assumption that they are available\n+# in the build environment for a reason (i.e., they are used).\n+for entry_point_name in ('sqlalchemy.dialects', 'sqlalchemy.plugins'):\n+    ep_datas, ep_hiddenimports = collect_entry_point(entry_point_name)\n+    datas += ep_datas\n+    hiddenimports += ep_hiddenimports\n+\n \n def hook(hook_api):\n     \"\"\"\n", "issue": "Failing to load sqlalchemy-plugin in pyinstaller'ed exe\nThis line of code works perfectly fine while running as a file from spyder.\r\n\r\nengine =  sqlalchemy.create_engine('teradata://uid:pwd@DBinstance?driver=Teradata')\r\nHowever, after making an exe file using pyinstaller, I get the following error while running the exe:\r\n\r\nsqlalchemy.exc.NoSuchModuleError: Can't load plugin:sqlalchemy.dialects:teradata\r\n\r\nThis looks to be an exe-creation-issue in loading libraries. Has anyone encountered a similar issue and was able to resolve? Any pointers would be appreciated.\n", "before_files": [{"content": "#-----------------------------------------------------------------------------\n# Copyright (c) 2005-2023, PyInstaller Development Team.\n#\n# Distributed under the terms of the GNU General Public License (version 2\n# or later) with exception for distributing the bootloader.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#\n# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)\n#-----------------------------------------------------------------------------\n\nimport re\nimport importlib.util\n\nfrom PyInstaller import isolated\nfrom PyInstaller.lib.modulegraph.modulegraph import SourceModule\nfrom PyInstaller.utils.hooks import check_requirement, logger\n\n# 'sqlalchemy.testing' causes bundling a lot of unnecessary modules.\nexcludedimports = ['sqlalchemy.testing']\n\n# Include most common database bindings some database bindings are detected and include some are not. We should\n# explicitly include database backends.\nhiddenimports = ['pysqlite2', 'MySQLdb', 'psycopg2', 'sqlalchemy.ext.baked']\n\nif check_requirement('sqlalchemy >= 1.4'):\n    hiddenimports.append(\"sqlalchemy.sql.default_comparator\")\n\n\n@isolated.decorate\ndef _get_dialect_modules(module_name):\n    import importlib\n    module = importlib.import_module(module_name)\n    return [f\"{module_name}.{submodule_name}\" for submodule_name in module.__all__]\n\n\n# In SQLAlchemy >= 0.6, the \"sqlalchemy.dialects\" package provides dialects.\n# In SQLAlchemy <= 0.5, the \"sqlalchemy.databases\" package provides dialects.\nif check_requirement('sqlalchemy >= 0.6'):\n    hiddenimports += _get_dialect_modules(\"sqlalchemy.dialects\")\nelse:\n    hiddenimports += _get_dialect_modules(\"sqlalchemy.databases\")\n\n\ndef hook(hook_api):\n    \"\"\"\n    SQLAlchemy 0.9 introduced the decorator 'util.dependencies'.  This decorator does imports. E.g.:\n\n            @util.dependencies(\"sqlalchemy.sql.schema\")\n\n    This hook scans for included SQLAlchemy modules and then scans those modules for any util.dependencies and marks\n    those modules as hidden imports.\n    \"\"\"\n\n    if not check_requirement('sqlalchemy >= 0.9'):\n        return\n\n    # this parser is very simplistic but seems to catch all cases as of V1.1\n    depend_regex = re.compile(r'@util.dependencies\\([\\'\"](.*?)[\\'\"]\\)')\n\n    hidden_imports_set = set()\n    known_imports = set()\n    for node in hook_api.module_graph.iter_graph(start=hook_api.module):\n        if isinstance(node, SourceModule) and node.identifier.startswith('sqlalchemy.'):\n            known_imports.add(node.identifier)\n\n            # Read the source...\n            with open(node.filename, 'rb') as f:\n                source_code = f.read()\n            source_code = importlib.util.decode_source(source_code)\n\n            # ... and scan it\n            for match in depend_regex.findall(source_code):\n                hidden_imports_set.add(match)\n\n    hidden_imports_set -= known_imports\n    if len(hidden_imports_set):\n        logger.info(\"  Found %d sqlalchemy hidden imports\", len(hidden_imports_set))\n        hook_api.add_imports(*list(hidden_imports_set))\n", "path": "PyInstaller/hooks/hook-sqlalchemy.py"}], "after_files": [{"content": "#-----------------------------------------------------------------------------\n# Copyright (c) 2005-2023, PyInstaller Development Team.\n#\n# Distributed under the terms of the GNU General Public License (version 2\n# or later) with exception for distributing the bootloader.\n#\n# The full license is in the file COPYING.txt, distributed with this software.\n#\n# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)\n#-----------------------------------------------------------------------------\n\nimport re\nimport importlib.util\n\nfrom PyInstaller import isolated\nfrom PyInstaller.lib.modulegraph.modulegraph import SourceModule\nfrom PyInstaller.utils.hooks import check_requirement, collect_entry_point, logger\n\ndatas = []\n\n# 'sqlalchemy.testing' causes bundling a lot of unnecessary modules.\nexcludedimports = ['sqlalchemy.testing']\n\n# Include most common database bindings some database bindings are detected and include some are not. We should\n# explicitly include database backends.\nhiddenimports = ['pysqlite2', 'MySQLdb', 'psycopg2', 'sqlalchemy.ext.baked']\n\nif check_requirement('sqlalchemy >= 1.4'):\n    hiddenimports.append(\"sqlalchemy.sql.default_comparator\")\n\n\n@isolated.decorate\ndef _get_dialect_modules(module_name):\n    import importlib\n    module = importlib.import_module(module_name)\n    return [f\"{module_name}.{submodule_name}\" for submodule_name in module.__all__]\n\n\n# In SQLAlchemy >= 0.6, the \"sqlalchemy.dialects\" package provides dialects.\n# In SQLAlchemy <= 0.5, the \"sqlalchemy.databases\" package provides dialects.\nif check_requirement('sqlalchemy >= 0.6'):\n    hiddenimports += _get_dialect_modules(\"sqlalchemy.dialects\")\nelse:\n    hiddenimports += _get_dialect_modules(\"sqlalchemy.databases\")\n\n# Collect additional dialects and plugins that are registered via entry-points, under assumption that they are available\n# in the build environment for a reason (i.e., they are used).\nfor entry_point_name in ('sqlalchemy.dialects', 'sqlalchemy.plugins'):\n    ep_datas, ep_hiddenimports = collect_entry_point(entry_point_name)\n    datas += ep_datas\n    hiddenimports += ep_hiddenimports\n\n\ndef hook(hook_api):\n    \"\"\"\n    SQLAlchemy 0.9 introduced the decorator 'util.dependencies'.  This decorator does imports. E.g.:\n\n            @util.dependencies(\"sqlalchemy.sql.schema\")\n\n    This hook scans for included SQLAlchemy modules and then scans those modules for any util.dependencies and marks\n    those modules as hidden imports.\n    \"\"\"\n\n    if not check_requirement('sqlalchemy >= 0.9'):\n        return\n\n    # this parser is very simplistic but seems to catch all cases as of V1.1\n    depend_regex = re.compile(r'@util.dependencies\\([\\'\"](.*?)[\\'\"]\\)')\n\n    hidden_imports_set = set()\n    known_imports = set()\n    for node in hook_api.module_graph.iter_graph(start=hook_api.module):\n        if isinstance(node, SourceModule) and node.identifier.startswith('sqlalchemy.'):\n            known_imports.add(node.identifier)\n\n            # Read the source...\n            with open(node.filename, 'rb') as f:\n                source_code = f.read()\n            source_code = importlib.util.decode_source(source_code)\n\n            # ... and scan it\n            for match in depend_regex.findall(source_code):\n                hidden_imports_set.add(match)\n\n    hidden_imports_set -= known_imports\n    if len(hidden_imports_set):\n        logger.info(\"  Found %d sqlalchemy hidden imports\", len(hidden_imports_set))\n        hook_api.add_imports(*list(hidden_imports_set))\n", "path": "PyInstaller/hooks/hook-sqlalchemy.py"}]}