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.
Option 1: Call the interpreter
python <filename>.py
python3 <filename>.py
Option 2: Let the script call the interpreter
#!/usr/bin/env python
.chmod +x <filename>.py
../<filename>.py
Just prefix the script's filename with
python
. E.g.:It's also worth mentioning that by adding a
-i
flag afterpython
, you can keep your session running for further coding. Like this: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:
... or any number of variations thereof
First run following command
Then at the top of the script, add
#!
and the path of the Python interpreter: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
:Change directories using
cd
to the directory containing the .py and run one of the following two commands:Alternatively run one of the following two commands:
Try using the command
python3
instead ofpython
. 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 topython3
(instead of replacingpython
) 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.