Running Python Programs
Table of Contents
In the last lesson, we learned how to install Python. Now let’s run Python code in two simple ways.
Two Ways to Run Python
Okay, so Python is installed. Now let’s actually run some code.
There are two ways to run Python. Let me show both. Then we can pick whichever feels right.
Way 1: Run Python in the Interactive Shell
This is what we used at the end of the last article. We type python in our terminal, and we get that >>> prompt. Then we type one line. Python runs it. We type another line. Python runs it. One at a time.
Like this:
>>> print("Hello")Hello>>> print("How are you")How are you>>> 2 + 35See? We type something. Python answers immediately. Like a conversation.
This is great for testing small things. Want to check what 5 * 7 is? Just type it. Want to see what a piece of code does? Just type it.
But here is the problem. Nothing is saved.
When we close the window, everything is gone. All those lines we typed? Gone. Like they never existed.
So this is good for practice. Good for quick tests. But not good for real programs.
Way 2: Run Python from a Script File
This is how real programs work. We write our code in a file. We save it. Then we run the whole file at once.
Let’s do this.
Step 1: Create a Python File
Open any text editor. Notepad, VS Code, whatever we have. Create a new file.
Type this:
print("Hello, this is my program")print("I am learning Python")print("This is fun")Step 2: Save the Python File
Save it as hello.py.
Now listen. This part is important. Make sure the file ends with .py, not .txt.
On Windows Notepad, we might need to change “Save as type” to “All Files” and then type hello.py. Otherwise it will save as hello.py.txt and that will not work.
Step 3: Open the Terminal in the Right Folder
We need to go to the folder where we saved the file.
On Windows:
cd path\to\your\folderOn Mac or Linux:
cd path/to/your/folderFor example, if we saved it on our Desktop:
cd DesktopStep 4: Run the Python File
Now type:
python hello.pyOr on some systems:
python3 hello.pyPress Enter.
We should see:
Hello, this is my programI am learning PythonThis is funThat’s it! We just ran a Python program from a file.
What Just Happened?
Let me explain. Python opened our file. It read it from top to bottom. Line by line. It ran each line. Then it stopped.
That is how every Python program works. Top to bottom. One line at a time.
No magic. No jumping around. Just straight down the file.
Interactive Shell vs Script File: A Real-Life Example
Think of it like this. The interactive shell is like talking to someone face to face. We say something. They reply. We say something else. They reply. It is a conversation.
The script file is like writing a letter. We write the whole thing. We send it. They read the whole thing and do everything we asked.
Both are useful. But for real work, we write letters. Not conversations.
Common Problems When Running Python Programs
Problem 1: “Python Is Not Recognized”
This means our computer cannot find Python.
On Windows, let’s try this instead:
py hello.pyThe py command often works even when python does not.
On Mac or Linux, let’s try:
python3 hello.pyProblem 2: “Can’t Open File ‘hello.py’”
This means we are in the wrong folder. Our terminal is looking for the file in the wrong place.
So what do we do? We use cd to go to the folder where we saved the file. Then we try again.
To check which folder we are in:
- On Windows: type
cd - On Mac or Linux: type
pwd
Problem 3: Nothing Happens
Let’s check if we saved the file with the .py extension. If it is hello.py.txt, Python will not run it.
Also let’s check for typos in the filename. hello.py and Hello.py are different on some systems.
Problem 4: We See an Error Message
Good! That means Python is working. It read our code and found something wrong.
Let’s read the error message. It usually tells us the line number and what the problem is. Do not panic. Errors are normal. Everyone gets them.
Another Way to Run Python
There is one more way. We can run a Python file from inside the Python shell.
Type python to enter the shell. Then type:
>>> exec(open("hello.py").read())This will run the file. But honestly, this is more complicated. Just use the terminal method. It is easier.
What Should We Use?
For learning and testing: use the interactive shell.
For real programs: use script files.
That is the simple rule.
Next, we will learn about Python IDEs and Editors so we can continue step by step.