Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

Add envinfo command to CLI for debugging purposes (#338)

Myles Scolnickc43d61f9792b
diff --git a/marimo/_cli/cli.py b/marimo/_cli/cli.py
index 3822bf032d..e902430f03 100644
--- a/marimo/_cli/cli.py
+++ b/marimo/_cli/cli.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import asyncio
import inspect
+import json
import logging
import os
import pathlib
@@ -15,6 +16,7 @@ import click
from marimo import __version__, _loggers
from marimo._ast import codegen
from marimo._cli import ipynb_to_marimo
+from marimo._cli.envinfo import get_system_info
from marimo._server.server import start_server
from marimo._utils.url import is_url
@@ -407,3 +409,14 @@ def convert(ipynb: str) -> None:
errors.
"""
print(ipynb_to_marimo.convert(ipynb))
+
+
+def env() -> None:
+ """Print out environment information for debugging purposes.
+
+ Example usage:
+
+ marimo env
+ """
+ print(json.dumps(get_system_info(), indent=2))
diff --git a/marimo/_cli/envinfo.py b/marimo/_cli/envinfo.py
new file mode 100644
index 0000000000..d50bf91164
--- /dev/null
+++ b/marimo/_cli/envinfo.py
@@ -0,0 +1,139 @@
+# Copyright 2023 Marimo. All rights reserved.
+from __future__ import annotations
+
+import json
+import platform
+import subprocess
+import sys
+from typing import Optional, Union
+
+from marimo import __version__
+
+
+def get_system_info() -> dict[str, Union[str, dict[str, str]]]:
+ info = {
+ "marimo": __version__,
+ "OS": platform.system(),
+ "OS Version": platform.release(),
+ "Python Version": platform.python_version(),
+ }
+
+ binaries = {
+ "Chrome": _get_chrome_version() or "--",
+ "Node": _get_node_version() or "--",
+ }
+
+ requirements = _get_pip_list()
+
+ return {**info, "Binaries": binaries, "Requirements": requirements}
+
+
+def _get_node_version() -> Optional[str]:
+ try:
+ process = subprocess.Popen(
+ ["node", "--version"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ text=True,
+ )
+ stdout, stderr = process.communicate()
+ if stderr:
+ return None
+ return stdout.strip().split()[-1]
+ except FileNotFoundError:
+ return None
+
+
+def _get_pip_list() -> dict[str, str]:
+ allowlist = [
+ "click",
+ "importlib_resources",
+ "jedi",
+ "markdown",
+ "pymdown-extensions",
+ "pygments",
+ "tomlkit",
+ "tornado",
+ "typing_extensions",
+ "black",
+ ]
+ try:
+ result = subprocess.run(
+ [
+ sys.executable,
+ "-m",
+ "pip",
+ "list",
+ "--format=json",
+ "--disable-pip-version-check",
+ ],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ )
+ packages = json.loads(result.stdout)
+ return {
+ package["name"]: package["version"]
+ for package in packages
+ if package["name"] in allowlist
+ }
+ except FileNotFoundError:
+ return {}
+
+
+def _get_chrome_version() -> Optional[str]:
+ def get_chrome_version_windows() -> Optional[str]:
+ process = subprocess.Popen(
+ [
+ "reg",
+ "query",
+ "HKEY_CURRENT_USER\\Software\\Google\\Chrome\\BLBeacon",
+ "/v",
+ "version",
+ ],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ text=True,
+ )
+ stdout, stderr = process.communicate()
+ if stderr:
+ return None
+ return stdout.strip().split()[-1]
+
+ def get_chrome_version_mac() -> Optional[str]:
+ process = subprocess.Popen(
+ [
+ "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
+ "--version",
+ ],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ text=True,
+ )
+ stdout, stderr = process.communicate()
+ if stderr:
+ return None
+ return stdout.strip().split()[-1]
+
+ def get_chrome_version_linux() -> Optional[str]:
+ process = subprocess.Popen(
+ ["google-chrome", "--version"],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.PIPE,
+ text=True,
+ )
+ stdout, stderr = process.communicate()
+ if stderr:
+ return None
+ return stdout.strip().split()[-1]
+
+ try:
+ if sys.platform.startswith("win32"):
+ return get_chrome_version_windows()
+ elif sys.platform.startswith("darwin"):
+ return get_chrome_version_mac()
+ elif sys.platform.startswith("linux"):
+ return get_chrome_version_linux()
+ else:
+ return None
+ except FileNotFoundError:
+ return None
diff --git a/marimo/_cli/test_envinfo.py b/marimo/_cli/test_envinfo.py
new file mode 100644
index 0000000000..e6036aa874
--- /dev/null
+++ b/marimo/_cli/test_envinfo.py
@@ -0,0 +1,29 @@
+# Copyright 2023 Marimo. All rights reserved.
+from marimo._cli.envinfo import (
+ _get_node_version,
+ _get_pip_list,
+ get_system_info,
+)
+
+
+def test_get_node_version() -> None:
+ node_version = _get_node_version()
+ assert node_version is None or isinstance(node_version, str)
+
+
+def test_get_pip_list() -> None:
+ pip_list = _get_pip_list()
+ assert isinstance(pip_list, dict)
+ assert "click" in pip_list
+ assert "tornado" in pip_list
+
+
+def test_get_system_info() -> None:
+ system_info = get_system_info()
+ assert isinstance(system_info, dict)
+ assert "marimo" in system_info
+ assert "OS" in system_info
+ assert "OS Version" in system_info
+ assert "Python Version" in system_info
+ assert "Binaries" in system_info
+ assert "Requirements" in system_info