Typing Stubs for Braintree
This package provides static type checking stubs for the `braintree` Python SDK. It allows tools like Mypy and Pylance to validate type usage in your Braintree integration code, improving code quality and catching potential errors before runtime. It's maintained as part of the `typeshed` project and is updated regularly to reflect changes in the official `braintree` library. The current version is `4.42.0.20260408`.
Common errors
-
ModuleNotFoundError: No module named 'braintree'
cause The actual `braintree` runtime library is not installed in your environment, only the type stubs.fixRun `pip install braintree` to install the core Braintree SDK. -
error: Library "braintree" has no attribute "Configuration" (reportMissingModuleStubs)
cause Your type checker (e.g., mypy, pylance) cannot find the type definition for `Configuration` within the `braintree` module. This usually means `types-braintree` is not installed, not correctly picked up, or is outdated relative to your `braintree` version.fixEnsure `types-braintree` is installed (`pip install types-braintree`) and your type checker is configured to use it. Also, verify both `braintree` and `types-braintree` are up-to-date. -
error: Name "Environment" is not defined
cause If this error comes from a type checker, it means the type information for `braintree.Environment` is not available. This can happen if `types-braintree` is missing or not properly linked by the type checker.fixInstall `types-braintree` (`pip install types-braintree`) to provide the necessary type definitions for Braintree enums and classes.
Warnings
- gotcha The `types-braintree` package only provides type hints for the `braintree` library. It does not provide any runtime functionality. You must install the `braintree` package itself for your code to run.
- gotcha Mismatching versions between `braintree` and `types-braintree` can lead to incorrect type checking results. Newer features in `braintree` might not yet have stubs, or outdated stubs might conflict with runtime changes.
- breaking Recent versions of `types-braintree` (and `typeshed` generally) require Python 3.10 or newer. Users on older Python versions will encounter installation errors or unexpected type checking behavior.
- gotcha When configuring Braintree, always use `braintree.Environment.Sandbox` for testing and `braintree.Environment.Production` with your actual production keys for live transactions. Using sandbox credentials in production or vice-versa will lead to errors.
Install
-
pip install braintree types-braintree
Imports
- Configuration
import braintree; braintree.Configuration(...)
- BraintreeGateway
import braintree; braintree.BraintreeGateway(...)
- Environment
import braintree; braintree.Environment.Sandbox
Quickstart
import braintree
import os
# Configure Braintree Gateway using environment variables for security
gateway = braintree.BraintreeGateway(
braintree.Configuration(
environment=os.environ.get('BRAINTREE_ENVIRONMENT', braintree.Environment.Sandbox),
merchant_id=os.environ.get('BRAINTREE_MERCHANT_ID', 'your_sandbox_merchant_id'),
public_key=os.environ.get('BRAINTREE_PUBLIC_KEY', 'your_sandbox_public_key'),
private_key=os.environ.get('BRAINTREE_PRIVATE_KEY', 'your_sandbox_private_key')
)
)
# Example: Generate a client token (common for web/mobile integrations)
try:
client_token = gateway.client_token.generate({})
print(f"Generated Client Token: {client_token}")
except Exception as e:
print(f"Error generating client token: {e}")