Running Python File in Terminal

Trying to learn how to run my scripts through Ubuntu's terminal regularly. That being said I am familiar with bash, wget, and awk being called but how do I call python files to run in the terminal? I would like to learn this but I am unsure on where to research it. I have a .pyw file that references several .py files in a folder.


Solution 1:

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

Solution 2:

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

python filename.py

Solution 3:

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>

Solution 4:

python <filename.py>

pyw should run in the same manner, I think. You can also start an interactive console with just

python

Also, you can avoid having to invoke python explicitly by adding a shebang at the top of the script:

#!/usr/bin/env python

... or any number of variations thereof