Python2代码在Python3运行报错:name 'onset_to_death' is not defined
NameError: name 'onset_to_death' is not defined Issue Hey there! I totally get how frustrating it is when you’ve worked through most Python 2→3 compatibility issues for your EpiSkills course, only to hit this roadblock. Let’s break down why this error is happening and how to fix it.
First, let’s clarify: this error means Python can’t find any variable, function, or object named onset_to_death in your current environment. It’s usually tied to one of a few common issues, especially in a Jupyter Notebook context.
Possible Causes & Fixes
1. You didn’t run the code that defines onset_to_death
Jupyter Notebooks run cells in the order you execute them—not just the top-to-bottom order they’re displayed. If the cell that creates onset_to_death (from your course materials) was skipped, or you ran cells out of sequence, the variable never got created.
- Fix: Go back to your course’s instructions and locate the exact code block that defines
onset_to_death. Make sure you run that cell first, and check that it finishes without errors. If that cell had a Python 2→3 compatibility bug (like usingprintas a statement instead of a function, orxrangeinstead ofrange), fix that first before re-running.
2. The definition code failed silently due to Python 2/3 differences
Sometimes code that works in Python 2 might throw a subtle error in Python 3 that stops onset_to_death from being created, but you might have missed the error message.
- Common compatibility issues to check in the definition code:
- If the code uses
iteritems()on a dictionary, replace it withitems()(Python 3’sitems()behaves like Python 2’siteritems()). - If it uses
raw_input(), swap it for Python 3’sinput(). - If it reads files with
open()without an encoding parameter, addencoding='utf-8'(e.g.,open('data.csv', 'r', encoding='utf-8')). - If it uses
unicode()to handle strings, replace it withstr().
- If the code uses
3. Scope or namespace issues
It’s possible onset_to_death was defined inside a function or a different namespace, and you’re trying to access it outside that scope. Or maybe you accidentally cleared your notebook’s namespace (e.g., using %reset or restarting the kernel without re-running all cells).
- Fix:
- Run the magic command
%whosin a new cell to list all variables currently in your environment. Ifonset_to_deathisn’t listed, you know the definition step failed. - If you restarted the kernel, re-run all cells from the top of the notebook to rebuild your environment.
- Run the magic command
4. Simple typo
Double-check that you’re spelling onset_to_death exactly the same way it was defined. A tiny typo (like onset_to_deathh or onset_to_deat) will trigger this error every time.
Quick Debugging Step
Add a cell right before the one that throws the error, and run this:
# Check if the variable exists try: print(onset_to_death) except NameError: print("onset_to_death is NOT defined—go back to its definition code!")
This will confirm whether the variable is missing, and point you straight to fixing the definition step.
内容的提问来源于stack exchange,提问作者arnold-c

