Running Python Programs from the Terminal: A Comprehensive Guide

Running Python programs from the terminal is a fundamental skill for every Python developer. The command-line interface provides a powerful way to execute Python scripts, offering flexibility and control over the execution process. In this article, we’ll explore various methods to run Python programs from the terminal with detailed examples.

1. Basic Python Script:

Let’s start with a simple Python script. Create a file named hello.py with the following content:

print("Hello, World!")

2. Navigating to the Script’s Directory:

Open your terminal and navigate to the directory where your Python script is located using the cd command:

cd path/to/your/script

3. Running Python Script:

Once in the script’s directory, you can run the script using the python command:

python hello.py

If you are using Python 3, you might need to use python3 instead:

python3 hello.py

4. Providing Command-Line Arguments:

If your script accepts command-line arguments, you can pass them after the script name:

python script.py arg1 arg2

5. Using Shebang to Make the Script Executable:

You can make your script executable without explicitly calling the python command by adding a shebang line at the beginning of the script. Modify your hello.py script:

#!/usr/bin/env python

print("Hello, World!")

Make the script executable:

chmod +x hello.py

Now, you can run the script directly:

./hello.py

6. Virtual Environments:

It’s good practice to use virtual environments to manage dependencies for your projects. Create a virtual environment:

python3 -m venv venv

Activate the virtual environment:

  • On Unix or MacOS: source venv/bin/activate
  • On Windows (Command Prompt): .\venv\Scripts\activate

Now, you can run your Python script within the virtual environment.

7. Running Python in Interactive Mode:

You can run Python interactively from the terminal by typing python or python3. This opens the Python REPL (Read-Eval-Print Loop), allowing you to execute Python commands interactively.

python
>>> print("Hello, Interactive Python!")

8. Executing Jupyter Notebooks:

If you work with Jupyter Notebooks, you can run them from the terminal using the jupyter command. First, install Jupyter:

pip install jupyter

Then, navigate to the directory containing your notebook and run:

jupyter notebook your_notebook.ipynb

9. Using Python’s -m Option:

You can also run Python modules as scripts using the -m option. For example, to run the unittest module:

python -m unittest discover tests

10. Running Python Scripts on Windows:

On Windows, you can use the Command Prompt or PowerShell to run Python scripts. The process is similar to Unix-like systems:

python hello.py
python hello.py

Conclusion:

Running Python programs from the terminal is an essential skill for any Python developer. Whether you are executing a simple script, working with virtual environments, or running interactive sessions, understanding the various methods and options available in the terminal empowers you to efficiently develop and test your Python code. As you explore more complex projects, you’ll find these skills invaluable for managing and executing your Python programs. Happy coding!

Leave a Comment