There are a few ways. Try these guides:
Making Pythona command takes the Main statement like the bottom of this sample: rhino-developer-samples/SampleCircleRadius.py at 7 · mcneel/rhino-developer-samples · GitHub
Or this: http://developer.rhino3d.com/guides/rhinopython/creating-rhino-commands-using-python/
Running python at startup looks like this: Script files to load when Rhino starts