Running Python File in Terminal

Option 1: Call the interpreter

  • For Python 2: python <filename>.py
  • For Python 3: python3 <filename>.py

Option 2: Let the script call the interpreter

  1. Make sure the first line of your file has #!/usr/bin/env python.
  2. Make it executable - chmod +x <filename>.py.
  3. And run it as ./<filename>.py

Just prefix the script's filename with python. E.g.:

python filename.py

It's also worth mentioning that by adding a -i flag after python, you can keep your session running for further coding. Like this:

python -i <file_name.py>