September 11, 2024

How to Run a Python Program

Running a Python program is straightforward and can be done in several ways depending on your environment and preferences. Below are the most common methods to run Python programs on your system.

1. Running Python Programs in an IDE

Integrated Development Environments (IDEs) like PyCharm, Visual Studio Code, and Jupyter Notebook provide a user-friendly way to write, edit, and run Python programs.

1.1. Using PyCharm

  1. Open PyCharm and create a new Python project.
  2. Right-click the project folder and select New > Python File to create a new Python script.
  3. Write your Python code in the editor.
  4. Right-click the Python file in the project explorer and select Run ‘filename’.
  5. The program will run, and you can see the output in the console at the bottom of the PyCharm window.

1.2. Using Visual Studio Code (VS Code)

  1. Open Visual Studio Code and create a new file with a .py extension.
  2. Install the Python extension for Visual Studio Code if you haven’t already.
  3. Write your Python code in the editor.
  4. Right-click anywhere in the editor and select Run Python File in Terminal.
  5. The program will run in the terminal at the bottom of the VS Code window.

1.3. Using Jupyter Notebook

  1. Open Jupyter Notebook from your terminal or Anaconda Navigator.
  2. Create a new notebook by selecting New > Python 3.
  3. Write your Python code in a cell.
  4. Press Shift + Enter to run the code in the cell.
  5. The output will be displayed directly below the cell.

2. Running Python Programs from the Command Line

You can also run Python programs directly from the command line or terminal.

2.1. Windows Command Prompt

  1. Open the Command Prompt by typing cmd in the Windows search bar and selecting the Command Prompt application.
  2. Navigate to the directory where your Python script is located using the cd command.
  3. Run the Python script by typing the following command and pressing Enter:
python filename.py

Replace filename.py with the name of your Python script.

2.2. macOS Terminal

  1. Open Terminal from the Applications > Utilities folder or by searching for “Terminal” in Spotlight.
  2. Navigate to the directory where your Python script is located using the cd command.
  3. Run the Python script by typing the following command and pressing Enter:
python3 filename.py

Replace filename.py with the name of your Python script. Note that macOS may require you to use python3 instead of python.

2.3. Linux Terminal

  1. Open Terminal from your application menu or by pressing Ctrl + Alt + T.
  2. Navigate to the directory where your Python script is located using the cd command.
  3. Run the Python script by typing the following command and pressing Enter:
python3 filename.py

Replace filename.py with the name of your Python script. Most Linux distributions require the use of python3 to run Python 3.x scripts.

3. Running Python Programs in an Interactive Shell

Python’s interactive shell (REPL – Read, Eval, Print, Loop) allows you to execute Python commands and see results immediately. This is useful for testing small code snippets or debugging.

3.1. Using Python Shell

  1. Open a terminal or command prompt.
  2. Type python (or python3 on macOS/Linux) and press Enter to start the interactive shell.
  3. Type your Python commands directly into the shell and press Enter to see the results.
  4. Exit the shell by typing exit() or pressing Ctrl + Z (Windows) or Ctrl + D (macOS/Linux).

4. Running Python Programs in a Text Editor

You can also write and run Python scripts using a simple text editor like Notepad (Windows) or TextEdit (macOS).

4.1. Using Notepad (Windows)

  1. Open Notepad and write your Python code.
  2. Save the file with a .py extension (e.g., hello.py).
  3. Open Command Prompt, navigate to the file’s directory, and run the script using python filename.py.

4.2. Using TextEdit (macOS)

  1. Open TextEdit and write your Python code.
  2. Save the file with a .py extension (e.g., hello.py).
  3. Open Terminal, navigate to the file’s directory, and run the script using python3 filename.py.

5. Running Python Programs in a Browser (Online Interpreters)

If you don’t want to install Python on your machine, you can use online Python interpreters to run your code directly in a web browser.

5.1. Example: Repl.it

  1. Go to Repl.it in your web browser.
  2. Create a new Python project or choose the Python language from the list of available templates.
  3. Write your Python code in the editor.
  4. Click the “Run” button to execute your code and see the output in the console.