ZhuLinsen/daily_stock_analysis · error · SystemExit

longbridge SDK is not installed. Run `pip install -r require

Error message

longbridge SDK is not installed. Run `pip install -r requirements.txt` first.

What it means

scripts/generate_longbridge_oauth_token.py wraps the import of longbridge.openapi in try/except and exits with this message when the SDK is absent or broken. It is an environment guard: the OAuth flow cannot even begin without the SDK, and the message directs you to install repo requirements. Note SystemExit(str) prints the message and exits with code 1.

Source

Thrown at scripts/generate_longbridge_oauth_token.py:49

        "--client-id",
        default=_default_client_id(),
        help="OAuth client_id. Defaults to LONGBRIDGE_OAUTH_CLIENT_ID, then LONGBRIDGE_APP_KEY.",
    )
    parser.add_argument(
        "--verify-symbol",
        default="",
        help="Optional Longbridge symbol such as AAPL.US or 700.HK to verify QuoteContext after auth.",
    )
    args = parser.parse_args()

    client_id = (args.client_id or "").strip()
    if not client_id:
        parser.error("missing --client-id or LONGBRIDGE_OAUTH_CLIENT_ID")

    try:
        from longbridge.openapi import Config, OAuthBuilder, QuoteContext
    except Exception as exc:
        raise SystemExit(
            "longbridge SDK is not installed. Run `pip install -r requirements.txt` first."
        ) from exc

    def show_url(url: str) -> None:
        print(f"Open this URL to authorize Longbridge OAuth:\n{url}\n")

    oauth = OAuthBuilder(client_id).build(show_url)
    config = Config.from_oauth(oauth)

    if args.verify_symbol:
        ctx = QuoteContext(config)
        quote = ctx.quote([args.verify_symbol])[0]
        print(f"Verified {args.verify_symbol}: {getattr(quote, 'last_done', None)}")

    token_cache = _token_cache_path(client_id)
    print(f"OAuth token cache: {token_cache}")
    print(
        "For GitHub Actions, store the base64 of this file as "

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Install deps into the active interpreter: pip install -r requirements.txt (or at minimum pip install longbridge).
  2. Verify the interpreter matches: which python / python -m pip install -r requirements.txt so packages and script share the venv.
  3. Test the import directly: python -c "import longbridge.openapi" — if that fails with an ImportError detailing the module, reinstall just that package.
  4. If the wheel is unavailable for your platform, check requirements.txt pins or the longbridge docs for supported Python versions.

Example fix

# before
$ python scripts/generate_longbridge_oauth_token.py --client-id xxx
longbridge SDK is not installed. Run `pip install -r requirements.txt` first.

# after
$ python -m pip install -r requirements.txt
$ python scripts/generate_longbridge_oauth_token.py --client-id xxx
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util

if importlib.util.find_spec("longbridge") is None:
    raise SystemExit("Install deps first: python -m pip install -r requirements.txt")

Type guard

def longbridge_available() -> bool:
    return importlib.util.find_spec("longbridge.openapi") is not None

Prevention

When it happens

Trigger: Running the script in a venv where requirements.txt was never installed, or where the longbridge wheel failed to install (platform mismatch); importing longbridge.openapi raising for a corrupted install; running under a different interpreter than the one you installed packages into.

Common situations: Fresh clone, script run before pip install -r requirements.txt; multiple Python environments (system vs venv) and the script picking the wrong one; longbridge SDK removed or renamed in a newer requirements snapshot.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/d4ff97cb774e109d. Report an issue: GitHub.