Skip to content

Running Python

Running Python

Once Python is installed and configured, there are several ways to run Python code. Below are the most commonly used methods to execute Python programs effectively:


1. Running Python in Interactive Mode (REPL)

What is Interactive Mode?

The interactive mode allows you to run Python commands one line at a time. It is useful for testing small code snippets.

How to Run:

  1. Open a terminal (Linux/macOS) or Command Prompt (Windows).
  2. Type:

python

or, for Python 3 specifically:

python3

Usage:

  • A prompt >>> will appear.
  • Type Python code directly and press Enter to execute. Example:

>>> print(“Hello, World!”)

Hello, World!

>>> 5 + 3

8

Exit Interactive Mode:

  • Type:

exit()

or press Ctrl+D.


2. Running Python Scripts from a File

What is Script Mode?

In script mode, you write Python code in a .py file and execute the entire file.

Steps to Run:

  1. Create a Python file (e.g., example.py):
    1. Use any text editor or IDE (e.g., VS Code, PyCharm, Notepad++) to write the code. Example content of example.py:

print(“This is a Python script!”)

  • Open the terminal or Command Prompt.
  • Navigate to the directory where the file is saved:

cd path/to/directory

  • Run the script:

python example.py

or, for Python 3:

python3 example.py


3. Running Python Using an Integrated Development Environment (IDE)

Popular IDEs for Python:

  1. PyCharm:
    1. A feature-rich IDE specifically for Python.
    1. Download from PyCharm Official Website.
  2. VS Code:
    1. A lightweight editor with Python extensions.
    1. Download from Visual Studio Code.
  3. IDLE:
    1. Python’s default IDE, installed automatically with Python.

How to Run:

  1. Open the IDE.
  2. Create or open a Python script.
  3. Press the Run button or use a shortcut:
    1. PyCharm: Shift+F10
    1. VS Code: Ctrl+F5
    1. IDLE: F5

4. Running Python in a Jupyter Notebook

What is Jupyter Notebook?

Jupyter Notebook provides an interactive environment for running Python code in cells, often used for data analysis and visualization.

How to Run:

  1. Install Jupyter Notebook:

pip install notebook

  • Start Jupyter Notebook:

jupyter notebook

  • Open the web interface and create a new notebook.
  • Write Python code in the cells and execute by pressing Shift+Enter.

5. Running Python in Online Interpreters

If Python is not installed on your system, you can use online interpreters to run Python code.

Popular Online Tools:

How to Run:

  1. Open the online interpreter.
  2. Paste or type your Python code.
  3. Press the Run button to execute.

6. Running Python in a Virtual Environment

Why Use Virtual Environments?

A virtual environment isolates dependencies for specific Python projects.

How to Run:

  1. Create a virtual environment:

python -m venv myenv

  • Activate the environment:
    • Windows:

myenv\Scripts\activate

  • macOS/Linux:

source myenv/bin/activate

  • Run Python:

python script_name.py


7. Running Python with Command-Line Arguments

What Are Command-Line Arguments?

You can pass parameters to your Python script via the command line.

How to Run:

  1. Write a Python script (args_example.py):

import sys

print(“Arguments passed:”, sys.argv)

  • Run the script with arguments:

python args_example.py arg1 arg2

  • Output:

Arguments passed: [‘args_example.py’, ‘arg1’, ‘arg2’]


8. Running Python in Docker

Why Docker?

Docker containers ensure consistent environments for Python applications.

How to Run:

  1. Install Docker.
  2. Pull a Python image:

docker pull python

  • Run a container:

docker run -it python

  • Use Python interactively within the container.

Conclusion Python offers multiple ways to run code, from the interactive shell to powerful IDEs and containerized environments. Choose the method that best suits your development needs, whether you’re a beginner experimenting with small scripts or a professional working on complex applications.