Write and Run Hello World in PyCharm
Introduction
In this chapter, you will write and run your first Python program inside PyCharm. Compared with a terminal-only workflow, PyCharm gives you syntax highlighting, code completion, and inline error feedback. This is important because it helps beginners build correct coding habits from the beginning.
Prerequisites
- Python
3.10+installed (3.12recommended) - PyCharm Community Edition installed
- Basic knowledge of folders and files
- Completed the previous chapter on editor installation
Why Start Hello World in PyCharm
Running Hello World in PyCharm is not just about printing a line of text. It helps you understand a full beginner workflow: create project, select interpreter, write code, and execute safely.
Key benefits:
- Cleaner learning environment for beginners
- Fewer syntax mistakes with real-time hints
- Faster troubleshooting through integrated tools
Tip
Best Practice
Use one project folder per learning stage. It keeps files organized and avoids confusion when selecting interpreters.
Step 1: Create a New PyCharm Project
- Open PyCharm.
- Click New Project.
- Choose a folder name such as
python_chapter7_hello. - Under interpreter settings, choose one option:
- Create a new virtual environment (recommended), or
- Use an existing interpreter.
- Click Create.
Step 2: Create hello_world.py
- In the project panel, right-click the project root.
- Select New -> Python File.
- Name the file
hello_world.
Add this code:
# Print a greeting message
print("Hello, World!")Step 3: Run the Program
You can run the file in either way:
- Right-click inside
hello_world.pyand choose Run 'hello_world' - Click the green run button near the top-right corner
Expected output in the Run panel:
Hello, World!
Step 4: Understand What PyCharm Did for You
When you ran the file, PyCharm automatically:
- Used the selected Python interpreter
- Executed the current file in the integrated run environment
- Displayed output and errors in a dedicated panel
This is exactly why IDE workflows are beginner-friendly.
Common Problems and Fixes
Problem: Nothing happens when clicking Run
- Likely cause: run configuration is missing or file is not saved.
- Fix:
- Save the file (
Ctrl+S/Cmd+S) - Right-click file and run again
- Check if a run configuration was created
- Save the file (
Problem: ModuleNotFoundError or interpreter mismatch
- Likely cause: wrong interpreter selected.
- Fix:
- Open Settings -> Project -> Python Interpreter
- Choose the correct interpreter or project virtual environment
- Re-run the script
Problem: Syntax looks wrong due to mixed quotes
- Likely cause: pasted content includes smart quotes.
- Fix:
- Delete the line
- Re-type using plain ASCII quotes
- Run again
Warning
Do not copy code from rich-text tools that may insert smart punctuation.
Always use plain text input for Python code.
Mini Practice
Try changing the message and run again:
# Print your own custom message
print("Hello from PyCharm Chapter 7!")If output appears correctly, your PyCharm Python workflow is ready.
FAQ
Should I use PyCharm or terminal to run beginner code?
Use both. Terminal builds foundational skills, while PyCharm improves productivity and readability. They complement each other.
Why do I need to select an interpreter in PyCharm?
Python code runs with a specific interpreter. Selecting the interpreter ensures your code executes in the correct environment.
Is hello_world.py naming required?
No, but it is a common and clear naming convention for first examples.
Can I skip virtual environments for this chapter?
Yes for a quick test, but using a project virtual environment is strongly recommended for clean and reproducible learning.