Python Code Execution

Running a Python program can be done in several ways, depending on how you’re interacting with Python and what your goals are (testing, scripting, web dev, etc.). The aim of this article is to teach you 2 important concept about using python in your coding journey. How to run a python program as well as what happens in the background when a python program is executed. Lets gets started

How to run Python Program?

You can run a Python program in lots of ways depending on what you’re trying to do. The simplest is just running it from the terminal with python yourfile.py, which works great for quick scripts or automation. If you’re coding regularly, using an IDE like VSCode or PyCharm gives you handy tools like debugging and code suggestions. For data science or learning, Jupyter Notebooks or Google Colab let you write and run code in chunks, often right in your browser. You can also schedule Python scripts to run automatically using cron jobs (on Linux) or Task Scheduler (on Windows). If you’re building a web app, you’ll likely use a framework like Flask or Django, which runs Python code behind a web server. And for sharing apps, you can package Python files into executables using tools like PyInstaller, so others can run them without installing Python. So really, whether you’re coding, automating, analyzing, or deploying—Python fits right in.

Running in Terminal (Command Line)

This is the most direct way to run python file. Assume you have a file called hello.py, open the terminal in the location where your python file is and just type python hello.py in the terminal and press enter to run it. Best for: Scripts, automation, small projects.

#hello.py
print("Hello, world!")

#to execute, goto terminal and enter
# python executable path file path example below
# python hello.py
# python /home/hello.py
# /usr/bin/python hello.py
# /usr/bin/python /home/hello.py

Using an IDE (Pycharm, Vscode etc)

You can write and run Python code inside an IDE. These environments give you auto-completion, debugging, and project management tools. Best for: Larger projects, debugging, team collaboration.

Using Python Interactive Shell

Just run python in your terminal, and you’ll be inside the Python interpreter. Best for: Quick tests, learning, exploring modules.

Using Notebooks

Used especially in data science and machine learning. It mixes code, comments, and charts. You’ll see a web interface where you can run Python in cells. Best for: Data analysis, reports, prototyping

How Python program is Executed in the background?

Python doesn’t compile into machine code like C or Java. Instead, it interprets your code step by step. Here’s what happens behind the scenes:

Your Python code (high-level) → Bytecode (intermediate) → Interpreter (CPython) → Execution (one step at a time)

Let’s walk through this with a real example and a real-world analogy.

Step 1: You Write the Python Code

def greet(name):
    print("Hello, " + name)

greet("Ashish")

This is human-readable code. You type this into a .py file, say hello.py.


Step 2: Python Translates It into Bytecode

When you run the file (python hello.py), Python first converts your code into something called bytecode. Bytecode is like simplified instructions for the Python interpreter — not for your computer’s processor, but for Python itself to understand.

🧠 Think of bytecode as the “middle language” that sits between English (your code) and machine code (your CPU understands). Example bytecode for the greet function might look like:

LOAD_NAME, CALL_FUNCTION, etc.

This bytecode is usually stored in .pyc files inside a __pycache__ folder, automatically created by Python.


Step 3: Python Virtual Machine (PVM) Executes the Bytecode

This is where the magic happens.

  • The Python Virtual Machine (PVM) is part of the interpreter (usually CPython).
  • It reads the bytecode line by line and executes it.

So when you run greet("Ashish"), the interpreter:

  1. Looks for the greet function
  2. Sends "Ashish" as an argument
  3. Executes the print() statement

It does all this line-by-line, which is why Python is called an interpreted language.

To wrap it up, running a Python program is as simple or as powerful as you need it to be. Whether you’re just getting started and running small scripts from the terminal, tinkering with ideas in a Jupyter notebook, or building something big with a web framework, Python gives you the flexibility to work your way. That’s one of the reasons it’s so popular—it doesn’t box you in. As you get more comfortable, you’ll find your favorite tools and workflows, and Python will grow with you every step of the way.

Leave a Comment

Share this