How to Run Python Code (Command Line and IDEs)
If you are new to Python, one of the first things to learn is how to actually run your code.
This page shows you how to:
- Run Python in the terminal for quick tests
- Run a saved
.pyfile from the command line - Run Python code in common IDEs and editors
- Understand when to use interactive mode and when to use script files
If you just want the fastest possible test, try this first:
python --version
python
print("Hello, world!")
exit()
# Save this as hello.py
print("Hello from a file")
# Run the file
python hello.py
If
pythondoes not work, trypython3instead. On some systems the Python launcherpyalso works, such aspy hello.pyon Windows.
What this page helps you do
After reading this page, you should be able to:
- Run Python interactively in a terminal
- Run a saved
.pyfile from the command line - Run Python code in an IDE or code editor
- Understand the difference between interactive mode and script files
If you have not installed Python yet, see how to install Python on Windows, macOS, and Linux.
Before you run Python code
First, make sure Python is installed.
Open a terminal or command prompt and try one of these commands:
python --version
If that does not work, try:
python3 --version
On Windows, this may also work:
py --version
You should see a version number, such as:
Python 3.12.2
If you get a message like command not found or python is not recognized, one of these is probably true:
- Python is not installed
- Python is installed but not added to your system
PATH - You need to use
python3orpyinstead ofpython
Run Python in interactive mode
Interactive mode lets you type Python code one line at a time.
This is useful for:
- Quick tests
- Trying small examples
- Checking how a function works
Start interactive mode
In your terminal, type:
python
Or:
python3
If it works, you will see the >>> prompt. That means Python is ready.
Example:
>>>
Try a simple example
Type this at the >>> prompt:
print("Hello, world!")
Expected output:
Hello, world!
You can also try simple math:
2 + 3
Expected output:
5
Exit interactive mode
To leave interactive mode:
- Type
exit()and press Enter - On Windows, press
Ctrl+Z, then Enter - On macOS/Linux, press
Ctrl+D
If you are learning basic Python syntax, your first Python program: Hello World explained is a good next step.
Run a Python file from the command line
For real programs, you usually save your code in a file ending with .py.
This is called a script file.
Step 1: Create a Python file
Create a file named hello.py and put this code in it:
print("Hello from a file")
Step 2: Open the terminal in the same folder
Your terminal needs to be in the folder that contains hello.py.
Step 3: Run the file
Use one of these commands:
python hello.py
Or:
python3 hello.py
Or on Windows:
py hello.py
Expected output:
Hello from a file
This is the normal way to run saved Python programs.
Example with input()
Scripts can also pause and wait for user input.
Save this as name.py:
name = input("What is your name? ")
print("Hello,", name)
Run it:
python name.py
Example session:
What is your name? Sam
Hello, Sam
If you want to understand this line better, see Python input() explained and Python print() explained.
How to open the correct folder in the terminal
A very common beginner problem is being in the wrong folder.
If Python says it cannot open your file, check where your terminal is currently located.
Move to a folder with cd
Use cd to change folders:
cd path/to/folder
Example:
cd Desktop/python-practice
Check the files in the current folder
On Windows:
dir
On macOS/Linux:
ls
You should see your Python file listed.
Check your current location
On macOS/Linux:
pwd
On Windows, cd by itself often shows the current folder:
cd
If your file is not in that folder, Python will not find it.
Example problem:
python hello.py
Possible error:
python: can't open file 'hello.py': [Errno 2] No such file or directory
This usually means:
- You are in the wrong folder
- The file name is wrong
- The file was not saved yet
Run Python code in an IDE or code editor
You do not have to use the terminal every time.
Most IDEs and code editors can run Python files with a Run button.
Common beginner tools include:
- IDLE
- VS Code
- PyCharm
The basic process is usually the same
- Open your Python file
- Make sure the file is saved
- Make sure the editor is using a valid Python interpreter
- Click Run or use the editor's run shortcut
Important checks
If the Run button does nothing, check these things:
- The file is saved
- The file name ends with
.py - Python is installed
- The editor is using the correct Python interpreter
- Your code does not contain a syntax error
For example, this code will fail because it is missing a closing parenthesis:
print("Hello"
That causes a SyntaxError: invalid syntax.
This code can also fail because of indentation problems:
name = "Sam"
if name == "Sam":
print("Hi")
That can cause an IndentationError: expected an indented block.
If your code runs in the terminal but not in your editor, the editor may be using a different Python installation.
Interactive mode vs running a script
These two ways of running Python are similar, but they are used for different purposes.
Interactive mode
Interactive mode:
- Runs one command at a time
- Shows results immediately
- Is good for quick testing
- Does not automatically save your code
Example:
>>> print("test")
test
>>> 10 * 2
20
Script file
A script file:
- Runs all saved code from top to bottom
- Can be saved and used again later
- Is better for larger programs
- Is better for repeatable work
Example hello.py:
print("Hello from a script")
print("This runs from top to bottom")
Expected output:
Hello from a script
This runs from top to bottom
For beginners, interactive mode is great for experiments, but script files are better for real practice.
Expected beginner examples to include
Here are three simple examples you can try right away.
Example 1: Interactive mode
Start Python:
python
Then type:
print("Hello, world!")
Expected output:
Hello, world!
Example 2: Run a saved file
Save this in hello.py:
print("Hello from a file")
Run it:
python hello.py
Expected output:
Hello from a file
Example 3: Script with user input
Save this in ask_name.py:
name = input("What is your name? ")
print("Nice to meet you,", name)
Run it:
python ask_name.py
Example output:
What is your name? Maya
Nice to meet you, Maya
Common problems and quick checks
If Python is not running correctly, check these common problems first.
python is not recognized or command not found
Possible causes:
- Python is not installed
- Python is not in
PATH - Your system uses
python3orpyinstead
Try:
python --version
python3 --version
py --version
Wrong file name or missing .py extension
Make sure the file name is exactly correct.
For example, hello.py is not the same as:
Hello.pyhellohello.py.txt
Running from the wrong folder
Use ls or dir to make sure your file is in the current folder.
Then run it again.
Using python when the system needs python3
Some systems separate Python 2 and Python 3 commands.
If python hello.py fails, try:
python3 hello.py
Interpreter not selected in the IDE
If the terminal works but your IDE does not, check the selected interpreter in the editor settings.
Syntax errors stop the script
If your script has invalid code, it will stop before finishing.
Common examples include:
- Missing
: - Missing
) - Wrong indentation
If you are stuck, see this beginner guide to debugging Python code.
Beginner debugging commands
These commands help you quickly check what is wrong.
python --version
python3 --version
py --version
pwd
cd path/to/folder
ls
dir
python hello.py
python3 hello.py
py hello.py
These commands can help you answer basic questions:
- Is Python installed?
- Which command works on this computer?
- Am I in the correct folder?
- Can Python find my file?
FAQ
Should I use python or python3?
Use the command that works on your system.
- Many Windows systems use
pythonorpy - Many macOS and Linux systems use
python3
What is the difference between the terminal and an IDE?
The terminal runs commands directly.
An IDE gives you:
- A code editor
- A Run button
- Built-in tools for writing and debugging code
Why does Python say it cannot open my file?
Usually one of these is the cause:
- You are in the wrong folder
- The file name is wrong
- The file was not saved
What does >>> mean in Python?
It is the prompt for Python interactive mode.
It means Python is ready for you to type code.
Can I run Python without saving a file?
Yes. Use interactive mode by typing python or python3 in the terminal.
Why does my code work in the terminal but not in my IDE?
Usually:
- The IDE is using a different Python interpreter
- The file was not saved before running it