How to Execute a Python File in Terminal

7 Answers 7

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

answered Jan 17 '13 at 20:26

2

  • what is the reason, that when I want to run my .py it works only work with your method option 1, and the second gives a syntax error?

    Oct 3 '18 at 9:11

  • what if you want to open a new python file??

    Mar 23 '19 at 3:44

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

              python filename.py                          

wjandrea

12.8k 4 gold badges 42 silver badges 87 bronze badges

answered Jan 17 '13 at 20:25

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>                          

answered Dec 6 '16 at 18:21

              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

answered Jan 17 '13 at 20:27

First run following command

              chmod +x <filename>.py                          

Then at the top of the script, add #! and the path of the Python interpreter:

              #!/usr/bin/python                          

If you would like the script to be independent of where the Python interpreter lives, you can use the env program. Almost all Unix variants support the following, assuming the Python interpreter is in a directory in the user's $PATH:

              #! /usr/bin/env python                          

wjandrea

12.8k 4 gold badges 42 silver badges 87 bronze badges

answered Jul 8 '14 at 17:41

Change directories using cd to the directory containing the .py and run one of the following two commands:

              python <filename>.py  # for Python 2.x   python3 <filename>.py # for Python 3.x                          

Alternatively run one of the following two commands:

              python /path/to/<filename>.py  # for Python 2.x   python3 /path/to/<filename>.py # for Python 3.x                          

answered Jun 8 '18 at 5:16

Try using the command python3 instead of python. If the script was written in Python3, and you try to run it with Python2, you could have problems. Ubuntu has both; changing the program name to python3 (instead of replacing python) made this possible. Ubuntu needs v2.7 (as of 2/16/2017) so DON'T delete or remove Python2, but keep them both. Make a habit of using Python3 to run scripts, which can run either.

wjandrea

12.8k 4 gold badges 42 silver badges 87 bronze badges

answered Feb 17 '17 at 0:19

2

  • -1 Python 3 cannot run Python 2 scripts (with few exceptions).

    Feb 17 '17 at 0:50

  • My bad, your right. Thats probably why Ubuntu still uses python 2. Sorry wjandrea, thanks for correcting me.

    Feb 18 '17 at 2:09

Not the answer you're looking for? Browse other questions tagged command-line python or ask your own question.

How to Execute a Python File in Terminal

Source: https://askubuntu.com/questions/244378/running-python-file-in-terminal

0 Response to "How to Execute a Python File in Terminal"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel