In the previous chapter, Code Style Guidelines, we learned how to write code that looks good and follows the rules.
But here is a hard truth about programming: You can follow all the rules and your code still might not work.
You might try to run a notebook, and it crashes. You might try to start the quiz app, and the screen stays blank. This is not a failure; it is part of the process. This chapter is about Troubleshootingβthe art of reading error messages and fixing the underlying problems.
When you are learning a new spoken language, you might say "I want apple" instead of "I would like an apple." People will still understand you.
Computers are not that smart. If you miss a single comma or install the wrong version of a tool, the computer will stop working and print a scary red error message.
This is the most common issue for beginners using Jupyter Notebooks.
The Situation: You open a lesson, click "Run" on the first code cell, and nothing happens. Or worse, a message pops up saying "The kernel has died, and will restart automatically."
The Goal: You need to revive the "Engine" (Kernel) so you can continue your lesson.
The Solution: We need to investigate the Environment. Usually, this happens because the Notebook is looking for a tool (library) that isn't installed in the specific "bubble" you are currently working in.
Troubleshooting is less about memorizing fixes and more about understanding where to look.
When Python crashes, it prints a "Stack Trace." This is a list of exactly what the computer was doing right before it died.
ModuleNotFoundError).As we learned in Key Technologies, the Kernel is the engine that runs your code. Sometimes the engine stalls. Sometimes you are trying to drive the car with the wrong engine (e.g., trying to run Python code with an R kernel).
This is like trying to put a diesel truck part into a small electric car. If the project asks for pandas version 1.3 but you have version 0.5, things will break.
Here are the specific fixes for the three main technologies in this course: Python, R, and the Web App.
Problem: You see ModuleNotFoundError: No module named 'pandas'.
The Fix: This means your "Shopper" (pip) didn't buy the ingredient. You need to verify you are in the correct virtual environment.
# 1. Check which pip you are using
which pip # (or 'where pip' on Windows)
# 2. If it doesn't say "ml-env", activate it!
source ml-env/bin/activate
# 3. Install the missing tool
pip install pandas
Explanation: Always check if (ml-env) is visible in your terminal prompt. If not, you are installing tools into the void, not your project.
Problem: The Kernel keeps dying or won't connect.
The Fix: Sometimes Jupyter gets confused about which Python to use. You can force it to recognize your environment.
# Install the kernel specification helper
pip install ipykernel
# Tell Jupyter to use your 'ml-env' as a kernel named "ML-Kernel"
python -m ipykernel install --user --name=ml-env --display-name "ML-Kernel"
Explanation: After running this, refresh your browser page. Click "Kernel" -> "Change Kernel" and select "ML-Kernel".
Problem: Error in library(tidyverse) : there is no package called 'tidyverse'.
The Fix: R packages are stored in a local library on your computer. If R can't find it, you just need to install it.
# Run this directly in the R console
install.packages("tidyverse")
# Try to load it again to verify
library(tidyverse)
Explanation: If this fails, you might need to run RStudio as an Administrator, as sometimes R doesn't have permission to write to the folder.
Problem: You run npm run serve (from Quiz Application Development) and get a giant wall of red text saying npm ERR!.
The Fix: Node.js dependencies can get corrupted easily. The best fix is the "Nuclear Option"βdelete the folder and start over.
# 1. Delete the node_modules folder (The "Nuclear" option)
# On Mac/Linux:
rm -rf node_modules package-lock.json
# 2. Re-install everything fresh
npm install
Explanation: node_modules can contain 30,000+ files. Sometimes one gets corrupted. Deleting the folder forces npm to download fresh, working copies of everything.
It helps to understand what happens "Under the Hood" when an error appears.
When you write bad code, the computer doesn't just stop; it "raises" a flag.
You can actually write code to handle these errors gracefully so your notebook doesn't crash. This is called try...except.
# A safe way to run risky code
try:
# Try to do the risky thing
import non_existent_library
except ImportError as e:
# If it fails, run this instead of crashing
print(f"Caught an error: {e}")
print("Please run: pip install non_existent_library")
Explanation: Instead of a red crash screen, the user sees a helpful message. Professional software uses this pattern everywhere.
In this chapter, we learned how to fix things when they break:
ml-env or node_modules.Now that we have fixed our environment and our code is running smoothly, we need to prove that it produces the correct answers. It is time to learn about testing.
Next Chapter: Testing and Validation
Generated by Code IQ