MCPcopy Create free account
hub / github.com/Datakitpage/Datakit / getInstalledPackages

Function getInstalledPackages

frontend/src/lib/python/init.ts:285–343  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

283 * Get list of installed packages
284 */
285export async function getInstalledPackages(): Promise<Map<string, string>> {
286 if (!pyodideInstance) {
287 return new Map();
288 }
289
290 try {
291 const result = await pyodideInstance.runPython(`
292 import sys
293 import json
294 import micropip
295
296 packages = {}
297
298 # Get packages from sys.modules (already imported)
299 for name, module in sys.modules.items():
300 if hasattr(module, '__version__'):
301 packages[name] = module.__version__
302 elif name in ['numpy', 'pandas', 'matplotlib', 'plotly', 'seaborn', 'scipy', 'requests', 'pillow', 'lxml', 'statsmodels']:
303 # For packages that might not have __version__ but are important
304 packages[name] = 'installed'
305
306 # Check for packages that are available but not yet imported
307 import importlib.util
308 common_packages = ['scipy', 'requests', 'pillow', 'lxml', 'statsmodels', 'plotly', 'seaborn']
309 for pkg_name in common_packages:
310 if pkg_name not in packages:
311 try:
312 spec = importlib.util.find_spec(pkg_name)
313 if spec is not None:
314 packages[pkg_name] = 'available'
315 except (ImportError, ModuleNotFoundError):
316 pass
317
318 # Also check installed packages via micropip
319 try:
320 installed = micropip.list()
321 for pkg in installed:
322 # micropip.list() returns a list of strings (package names)
323 if isinstance(pkg, str) and pkg not in packages:
324 packages[pkg] = 'installed'
325 # Handle case where it might return dict-like objects
326 elif hasattr(pkg, 'get'):
327 pkg_name = pkg.get('name', '')
328 pkg_version = pkg.get('version', 'installed')
329 if pkg_name and pkg_name not in packages:
330 packages[pkg_name] = pkg_version
331 except Exception as e:
332 print(f"Could not get micropip package list: {e}")
333
334 json.dumps(packages)
335 `);
336
337 const packagesObj = JSON.parse(result);
338 return new Map(Object.entries(packagesObj));
339 } catch (error) {
340 console.error("[Python] Failed to get installed packages:", error);
341 return new Map();
342 }

Callers 1

pythonStore.tsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected