The right way to do this is to import a .py file into Blender. If instead you have a block of text, you will have to paste it into Notepad or other text editing software, and save it with the .py extension. For this you will have to change “save as type” to “All Files”:
In your blender file, select the “scripting” view:
To the left of the 3d view there will be a blank window. Click to open the “Text” menu and select “open text block”. (Alternatively you can press ALT O with your mouse hovered over this window):
Browse for and find the .py file. There should now be an option to “run script”. Click it!
If you get any error messages, you can get more information by opening the system console: