Skip to content

Commit

Permalink
fix: use importlib.util.find_spec instead of attempting costly import…
Browse files Browse the repository at this point in the history
…s at startup, reducing startup time by ~1s
  • Loading branch information
ErikBjare committed Oct 10, 2024
1 parent 04b3109 commit 7b75520
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions gptme/tools/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import dataclasses
import functools
import importlib.util
import re
import types
from collections.abc import Callable, Generator
Expand Down Expand Up @@ -43,6 +44,9 @@
def register_function(func: T) -> T:
"""Decorator to register a function to be available in the IPython instance."""
registered_functions[func.__name__] = func
# if ipython is already initialized, push the function to it to make it available
if _ipython is not None:
_ipython.push({func.__name__: func})
return func


Expand Down Expand Up @@ -150,17 +154,15 @@ def get_installed_python_libraries() -> set[str]:
"matplotlib",
"seaborn",
"scipy",
"scikit-learn",
"sklearn",
"statsmodels",
"pillow",
"PIL",
]
installed = set()
for candidate in candidates:
try:
__import__(candidate)
if importlib.util.find_spec(candidate):
installed.add(candidate)
except ImportError:
pass

return installed


Expand Down

0 comments on commit 7b75520

Please sign in to comment.