How to run my python code when motion sensor is activated?

have a look at this post Take picture when motion with Pi 3 and Agent 2.0 CayenneMQTTClient() and notify by email - how to trigger a script on Pi 3