📌 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
And it prints:
Weather in Karachi:
Temperature: 32.4°C
Wind speed: 12.5 km/h
No Frameworks. No GUI. Just Python + an API.
3. What You’ll Need
- Python Installed
- The requests library:
pip install requests
- 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"]
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"]
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()
8. Run It
python weather.py Karachi
Output:
Weather in Karachi:
Temperature: 32.4°C
Wind speed: 12.5 km/h
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)