CurrencyConverter
CurrencyConverter is a Python library that provides historical currency exchange rates, primarily using data from the European Central Bank (ECB). It allows conversion between various currencies based on specified dates or the latest available historical rate. The library is compatible with Python 3.9+ and is actively maintained, with the current version being 0.18.16.
Warnings
- gotcha The default `CurrencyConverter()` constructor uses embedded historical data that is packaged with the library and might be significantly outdated. It does not query real-time APIs. For up-to-date historical rates, you must explicitly initialize it with `ECB_URL` (e.g., `CurrencyConverter(ECB_URL)`), which downloads the latest full historical data from the European Central Bank upon initialization.
- gotcha The ECB data primarily provides rates against the Euro (EUR) and starts from January 1, 1999. Requesting conversions for dates before 1999 will raise a `RateNotFoundError` by default.
- gotcha A `RateNotFoundError` can occur if specific currency rates are missing for a requested date within the available data range. This is distinct from requesting dates outside the overall data bounds.
Install
-
pip install currencyconverter
Imports
- CurrencyConverter
from currency_converter import CurrencyConverter
- ECB_URL
from currency_converter import ECB_URL
Quickstart
from currency_converter import CurrencyConverter, ECB_URL
from datetime import date
import os
# Initialize converter with the latest historical data from ECB (requires internet connection)
# This downloads the full history, which can take a moment.
# If you prefer to use the embedded data (might be outdated), use `c = CurrencyConverter()`
c = CurrencyConverter(ECB_URL)
# Convert 100 USD to EUR using the latest available rate
amount_eur = c.convert(100, 'USD', 'EUR')
print(f"100 USD = {amount_eur:.2f} EUR (latest historical rate)")
# Convert 50 EUR to GBP on a specific date
specific_date = date(2020, 1, 15)
amount_gbp = c.convert(50, 'EUR', 'GBP', date=specific_date)
print(f"50 EUR on {specific_date} = {amount_gbp:.2f} GBP")
# List available currencies (e.g., to check supported codes)
# print(c.currencies)