For Python 2:
>>> execfile('filename.py') For Python 3:
>>> exec(open("filename.py").read()) # or >>> from pathlib import Path >>> exec(Path("filename.py").read_text()) See the documentation. If you are using Python 3.0, see this question.
See answer by @S.Lott for an example of how you access globals from filename.py after executing it.