It's also worth noting that this will only work when run within the QGIS Python environment.