Run Python Script in Terminal

Python is a very flexible programming language. We can easily write the required code in a plain-text file and give it a .py extension to make it a Python script file. We also have python notebook files with the extension .ipynb extension, which can be run in software like Jupyter Notebook.

To run Python scripts, we can make use of IDEs or the device terminal.

For running Python scripts on the terminal, we have to install Python on the device first.

After installing Python and creating the required script, we can easily run it on the terminal using the python command. For example, with the following command, we run a Python script.

python hello.py

For using this, we need to open the terminal in the directory where the file is stored. If we have installed Python v3 and above, we need to change the python command to python3.

We can go to the required directory where the file is stored using the cd command in the terminal. If we want to avoid this, we can mention the file’s directory with the command as shown below.

python /Users/Python/hello.py

It is also possible to run small code lines using the python -c "<required code>" command.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.