Build a real Python app that fetches live weather data from any city in the world. You'll learn to call APIs, parse JSON, and handle user input — three core skills used in almost every Python project.
A command-line Python application where users type a city name and instantly see the current temperature, weather condition (clear, cloudy, rainy), humidity, and wind speed — pulled live from a free weather API.
By the end of this tutorial, you'll have a working weather app and you'll understand exactly how the web works behind the scenes. Let's get started.
Create a new folder for your project anywhere on your computer. Open it in VS Code. Inside, create a new file called weather.py — this is where all your code will live.
Open a terminal in VS Code (Ctrl + ` on Windows, Cmd + ` on Mac) and verify Python is installed:
You should see something like Python 3.11.0. If you get an error, install Python from python.org first.
requests libraryTo talk to the weather API, we need a Python library called requests — the most popular tool for making HTTP requests. Install it with pip:
You'll see "Successfully installed requests" once it's done. This library lets your Python code fetch data from any website or API on the internet.
pip doesn't work, try pip3 or python -m pip install requests. These are common alternatives on different systems.
Weather data comes from an API (Application Programming Interface) — a service that gives your program data on demand. We'll use OpenWeatherMap, which has a generous free tier.
Open weather.py and start with this code. Paste your API key where it says YOUR_API_KEY_HERE.
Three things are happening: we ask the user for a city, build a URL pointing to the API, then requests.get() fetches the data and .json() converts it into a Python dictionary.
In your terminal, run:
Type a city like London and press Enter. You'll see a big block of JSON data:
This is the raw weather data. Now we just need to pull out the parts we care about and display them nicely.
Replace the last print(data) line with this — it extracts the useful information and displays it cleanly:
Save and run again. Type a city. You should see a clean, formatted weather report.
What if the user types a city that doesn't exist? Or has no internet? Right now the program crashes. Let's fix that with a try/except block. Replace your entire code with this final version:
Now your app handles three real-world situations: invalid cities, network failures, and successful requests. That's the same pattern used in professional Python applications.
You just built a real Python app that talks to the internet, parses live data, and handles errors gracefully — the same skills used in every modern Python project.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.