How to Solve NameError: name 'bpy' is not defined

Solution: Import the 'bpy' module

# Add the following line to the top of your code
import bpy

 

For more information:
Python bpy