OpenBB-finance/OpenBB · error · ImportError
Missing required package: {missing_package}. Please install
Error message
Missing required package: {missing_package}. Please install dependencies: pip install pycountry beautifulsoup4 requests What it means
An ImportError raised at import time of openbb_core/provider/utils/update_country_data.py, a maintenance script that scrapes country data with requests + BeautifulSoup and pycountry. If either pycountry or beautifulsoup4 is missing, the except ImportError block extracts the offending package name and re-raises with install instructions. It fails before any code in the module can run.
Source
Thrown at openbb_platform/core/openbb_core/provider/utils/update_country_data.py:37
"""
from __future__ import annotations
import argparse
import json
import re
import unicodedata
from datetime import date
from pathlib import Path
import requests
try:
import pycountry
from bs4 import BeautifulSoup
except ImportError as e:
missing_package = str(e).split("'")[1] if "'" in str(e) else "unknown"
raise ImportError(
f"Missing required package: {missing_package}. "
"Please install dependencies: pip install pycountry beautifulsoup4 requests"
) from e
SCRIPT_DIR = Path(__file__).parent
DEFAULT_OUTPUT = SCRIPT_DIR / "country_data.json"
HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
)
}
# Expected member counts for sanity checks (approximate — update when membership changes)
EXPECTED_COUNTS: dict[str, int | tuple[int, int]] = {
"G7": 7,
"G20": 20, # 19 countries + EU (we only count countries)View on GitHub (pinned to 3e071fcc2c)
Solutions
- Install the extras: pip install pycountry beautifulsoup4 requests.
- If it was pulled in by a module scan (e.g. pytest collection), either install the deps or exclude the script from collection.
- For library consumers: don't import this module; it is a data-generation utility, not runtime API - guard imports behind a try/except or optional extra.
- After installing, verify with python -c 'import pycountry, bs4'.
Example fix
# before from openbb_core.provider.utils.update_country_data import update_country_data # ImportError # after (install first: pip install pycountry beautifulsoup4 requests) from openbb_core.provider.utils.update_country_data import update_country_data
Defensive patterns
Strategy: try-catch
Validate before calling
import importlib.util
for pkg in ('pycountry', 'bs4', 'requests'):
if importlib.util.find_spec(pkg) is None:
raise ImportError(f'install {pkg} first: pip install pycountry beautifulsoup4 requests') Type guard
def can_import_update_country_data() -> bool:
import importlib.util
return all(importlib.util.find_spec(p) for p in ('pycountry', 'bs4', 'requests')) Try / catch
try:
from openbb_core.provider.utils.update_country_data import update_country_data
except ImportError as e:
if 'Missing required package' in str(e):
subprocess.run([sys.executable, '-m', 'pip', 'install', 'pycountry', 'beautifulsoup4', 'requests'], check=True)
from openbb_core.provider.utils.update_country_data import update_country_data Prevention
- Install the scraping extras in environments that run data-refresh scripts.
- Never import maintenance scripts from library code; keep them behind optional extras.
When it happens
Trigger: Importing update_country_data (directly or via tooling that scans all modules) in an environment where 'pip install pycountry beautifulsoup4 requests' has not been run - e.g. a minimal install of openbb-core without the scraping extras, or a CI venv pruning optional dependencies.
Common situations: Running the country-data refresh script on a fresh machine; CI environments that only install core requirements; dependency conflicts where pip uninstalled bs4/pycountry during a resolve.
Related errors
- Unsupported file format. Please use .json or .env files.
- Failed to get Jupyter URL
- pywry is not installed
- Error adding trend line: {e}
- Trace '{trace}' not found
AI-assisted analysis of OpenBB-finance/OpenBB@3e071fcc2c (2026-08-14).
Data as JSON: /api/errors/93e97d9611a6c162.
Report an issue: GitHub.