Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 36 down vote accepted
python <filename>.py


Make sure the first line of your file has #!/usr/bin/env python.

Make it executable - chmod +x <filename>.py.

And run it as ./<filename>.py

share|improve this answer
where does chmod +x <filename>.py go? in Terminal? in the script? (If in the script, where?) – tofu_bacon Sep 20 '15 at 17:30
@tofu_bacon In the terminal. – abhshkdz Sep 20 '15 at 17:33
I keep getting an invalid syntax error. I've doubled check waht I'm typing. Any clue what it could be? – tofu_bacon Sep 20 '15 at 17:39
What's the exact command you're typing? – abhshkdz Sep 20 '15 at 17:40
This looks fine. Something's wrong in your python code. – abhshkdz Sep 20 '15 at 17:44

Just prefix the script's filename with python


share|improve this answer
python <>

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


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

share|improve this answer

First run following command

chmod +x <filename>.py

Then add the path of python interpreter and put #! front of the path.

#!/usr/bin/python or #!/usr/local/bin/python

using the pathname for where the Python interpreter is installed on your platform.

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 on the user’s $PATH:

#! /usr/bin/env python
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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