DEV Community

Sameer Qaiser
Sameer Qaiser

Posted on

Building a Weather CLI App in Python (Beginner Project)

๐Ÿ“Œ Quick Info

  • Topic: Building a real command-line app with Python
  • Target Audience: Beginners who know the basics and want a first project
  • Goal: Combine everything you've learned into one working tool

1. Introduction

"I'd learned variables, loops, functions, APIs, and JSON. But I hadn't built anything real. So I built a weather app โ€” and it taught me more than any tutorial."


2. What We're Building

A command-line tool where you type:

python weather.py Karachi
๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung

And it prints:

Weather in Karachi:
Temperature: 32.4ยฐC
Wind speed: 12.5 km/h
๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung

No Frameworks. No GUI. Just Python + an API.


3. What Youโ€™ll Need

  • Python Installed
  • The requests library:
pip install requests
๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung
  • An idea of how APIs work (see my earlier article)

4. The Weather API

We'll use Open-Meteo because it's free and doesn't require an API key.

It takes latitude and longitude, and returns weather data.

But first, we need to turn a city name into coordinates. For that, we'll use their geocoding endpoint.


5. Step 1: Get Coordinates from a City Name

import requests

def get_coordinates(city):
    url = "https://geocoding-api.open-meteo.com/v1/search"
    params = {"name": city, "count": 1}
    response = requests.get(url, params=params)
    data = response.json()

    if "results" not in data:
        return None

    result = data["results"][0]
    return result["latitude"], result["longitude"]
๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung

6. Step 2: Get the Weather

def get_weather(lat, lon):
    url = "https://api.open-meteo.com/v1/forecast"
    params = {
        "latitude": lat,
        "longitude": lon,
        "current_weather": True
    }
    response = requests.get(url, params=params)
    return response.json()["current_weather"]
๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung

7. Step 3: Put It Together

import sys

def main():
    if len(sys.argv) < 2:
        print("Usage: python weather.py <city>")
        return

    city = sys.argv[1]
    coords = get_coordinates(city)

    if not coords:
        print(f"Couldn't find city: {city}")
        return

    weather = get_weather(*coords)
    print(f"\nWeather in {city}:")
    print(f"Temperature: {weather['temperature']}ยฐC")
    print(f"Wind speed: {weather['windspeed']} km/h")

if __name__ == "__main__":
    main()
๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung

8. Run It

python weather.py Karachi
๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung

Output:

Weather in Karachi:
Temperature: 32.4ยฐC
Wind speed: 12.5 km/h
๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung ๐Ÿ“บ Info Siaran: sameerqaiser/building a weather cli app in python beginner project f - Jadwal Siaran Langsung

Thatโ€™s it. A working weather app in 30 Lines.


9. What I Learned Building This

ยท APIs are just URLs with parameters. Once I saw that, everything clicked.
ยท Functions keep things clean. Without get_coordinates and get_weather, the file would be a mess.
ยท sys.argv is how you take input from the terminal. Simple, but I'd never used it before.
ยท Error handling matters. What if the city doesn't exist? What if the API is down? Always check.
ยท Small projects teach more than big tutorials. This one taught me more than three courses combined.


10. What To Build Next

Once you have this working, try:

ยท Add error handling for network failures
ยท Save favorite cities to a JSON file
ยท Format the output with colors (using colorama)
ยท Add a forecast instead of just current weather
ยท Turn it into a web app with Flask or FastAPI


11. Conclusion

"Building this tiny app taught me more than reading ten articles about APIs. If you're stuck in tutorial hell, pick something small and build it. You'll learn the syntax by breaking it. That's the whole game.โ€

Top comments (0)