Fixing the ‘Module Has No Attribute’ Error When Importing qrcode
in Python
Introduction
Encountering the error AttributeError: module 'qrcode' has no attribute 'QRCode'
when working with Python’s qrcode
module? This is a common issue caused by naming conflicts. In this blog, we’ll explore why this happens and how to fix it.

Understanding the Problem
The error typically looks like this:
import qrcode
qr_code = qrcode.QRCode()
Error Output:
AttributeError: module 'qrcode' has no attribute 'QRCode'
This happens because Python is mistakenly importing a local file named qrcode.py
instead of the actual qrcode
library.
How to Fix It
1. Rename Your Script
If your script is named qrcode.py
, Python treats it as the qrcode
module, causing conflicts. Rename your script to something else, such as:
✅ Correct File Names:
generate_qr.py
qr_generator.py
my_qrcode_script.py
2. Delete Cached Files
Even after renaming, Python might still use cached imports. To fix this, delete the __pycache__
directory inside your project:
- On macOS/Linux, run:
rm -rf __pycache__
- On Windows, manually delete the
__pycache__
folder from your project directory.
3. Run Your Script Again
Now, retry running your script:
python generate_qr.py
Your script should now work correctly.
Best Practices to Avoid This Issue
✔ Avoid naming files after Python libraries (e.g., pandas.py
, numpy.py
, requests.py
). ✔ Use meaningful names for scripts (e.g., qr_generator.py
instead of qrcode.py
). ✔ Use Virtual Environments to manage dependencies properly.
Conclusion
This error is a simple yet common mistake that can be quickly fixed by renaming files and clearing cached imports. By following best practices, you can avoid similar issues when working with Python libraries.
Now go ahead and generate those QR codes without any hassle! 🚀
GitHub Repository
For the complete code and examples, visit my GitHub repository: Python QR Code Generator