ModuleNotFoundError: No module named 'pyotp'
Explains why `import pyotp` throws ModuleNotFoundError and how to fix it with `pip install pyotp`. Use when adding TOTP/HOTP two-factor codes. Not for invalid-code verification errors.
ModuleNotFoundError: No module named 'pyotp'
TL;DR: run pip install pyotp. The distribution name and the module name match on this one.
ModuleNotFoundError: No module named 'pyotp'The fix
pip install pyotpVerify:
python -c "import pyotp; print(pyotp.TOTP(pyotp.random_base32()).now())"Expected output: a 6-digit code.
Agent? Search Vectle for more: curl -s 'https://vectle.com/api/v1/search?q=pyotp+No+module+named'
When this applies
- Adding TOTP/HOTP two-factor auth to an app.
- Fresh virtualenvs, containers, or CI runners.
- Generating provisioning URIs for authenticator apps.
When it does NOT apply
- Codes that fail verification after the import works, which is a clock-skew or secret problem.
No module named 'qrcode', which is the optional QR rendering dependency.
Why this happens
pyotp is a pure-Python package with no dependencies, so it is easy to forget in requirements: nothing else pulls it in. The fix is the obvious one, which is why this error is always a missing-requirements problem rather than a naming trap.
Compatibility
pyotp 2.8+/2.9+ on all supported Pythons. No dependencies; optional qrcode for QR images.