forex-python

1.9.2 · active · verified Tue Apr 14

forex-python (version 1.9.2) is an active Python library providing free foreign exchange rates and currency conversion functionalities. It supports listing all currency rates, retrieving Bitcoin prices, converting amounts between currencies, and accessing historical rates since 1999. The library's core data comes from theratesapi.com (sourced from the European Central Bank) for currency rates and CoinDesk for Bitcoin prices. It is regularly updated, though with an irregular release cadence, and is compatible with Python versions >=2.7 and <4, excluding 3.0-3.2.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fetch current exchange rates, convert amounts, get Bitcoin prices, and retrieve currency symbols using the `forex-python` library. It emphasizes using `force_decimal=True` for accurate financial calculations.

from decimal import Decimal
from forex_python.converter import CurrencyRates, CurrencyCodes
from forex_python.bitcoin import BtcConverter

# Initialize CurrencyRates with force_decimal for precision
c = CurrencyRates(force_decimal=True)

# Get conversion rate from USD to INR
usd_to_inr_rate = c.get_rate('USD', 'INR')
print(f"1 USD = {usd_to_inr_rate} INR")

# Convert an amount
amount_usd = Decimal('100.50')
converted_amount_inr = c.convert('USD', 'INR', amount_usd)
print(f"{amount_usd} USD = {converted_amount_inr} INR")

# Get all latest rates for a base currency
all_usd_rates = c.get_rates('USD')
# print(f"All USD rates: {all_usd_rates}") # Uncomment to see all rates

# Get Bitcoin price
b = BtcConverter(force_decimal=True)
btc_usd_price = b.get_latest_price('USD')
print(f"1 BTC = {btc_usd_price} USD")

# Get currency symbol
codes = CurrencyCodes()
gbp_symbol = codes.get_symbol('GBP')
print(f"GBP symbol: {gbp_symbol}")

view raw JSON →