How to Run Python Script in Terminal

Manav Narula Feb 02, 2024
How to 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.

Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn