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.12 recommended)
  • 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

  1. Open PyCharm.
  2. Click New Project.
  3. Choose a folder name such as python_chapter7_hello.
  4. Under interpreter settings, choose one option:
    • Create a new virtual environment (recommended), or
    • Use an existing interpreter.
  5. Click Create.

Step 2: Create hello_world.py

  1. In the project panel, right-click the project root.
  2. Select New -> Python File.
  3. Name the file hello_world.

Add this code:

python
# 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.py and 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:
    1. Save the file (Ctrl+S / Cmd+S)
    2. Right-click file and run again
    3. Check if a run configuration was created

Problem: ModuleNotFoundError or interpreter mismatch

  • Likely cause: wrong interpreter selected.
  • Fix:
    1. Open Settings -> Project -> Python Interpreter
    2. Choose the correct interpreter or project virtual environment
    3. Re-run the script

Problem: Syntax looks wrong due to mixed quotes

  • Likely cause: pasted content includes smart quotes.
  • Fix:
    1. Delete the line
    2. Re-type using plain ASCII quotes
    3. 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:

python
# 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.