Python导入自定义模块报错求助:文本游戏开发拆分文件遇问题
Hey there! Let's work through this module import bug you're hitting with your text-based adventure game—sounds like a cool project, by the way! When you split out your armor/weapon code into my_character.py and tried importing it into your intro.py script, getting "undefined" errors usually boils down to a few common issues. Let's break them down:
1. Double-check your file setup
- Make sure
my_character.pyis in the exact same folder asintro.py. Python looks for modules in the current working directory by default, so if they're in separate folders, it won't find the module unless you add the folder path to your Python path. - Verify the filename spelling! Typos like
my_charactor.pyinstead ofmy_character.pyare super easy to miss, and Python cares about exact matches (especially on Linux/macOS where filenames are case-sensitive).
2. Fix your import syntax
If you're trying to use items directly from my_character without referencing the module, you need to import them explicitly. For example:
- If your
my_character.pyhas:# my_character.py iron_sword = {"name": "Iron Sword", "damage": 8} class LeatherArmor: def __init__(self): self.defense = 5 self.name = "Leather Armor" - Then in
intro.py, you have two options:- Import the whole module and reference items with the module prefix:
import my_character # Use the sword and armor like this print(my_character.iron_sword["name"]) player_armor = my_character.LeatherArmor() - Import specific items directly:
from my_character import iron_sword, LeatherArmor # Now you can use them without the module prefix print(iron_sword["name"]) player_armor = LeatherArmor()
- Import the whole module and reference items with the module prefix:
If you only do import my_character and try to use iron_sword directly, Python will throw an "undefined" error—this is one of the most common mistakes!
3. Watch out for circular imports
If my_character.py is also importing something from intro.py (like a player class or game state), you'll create a circular import. This messes up Python's module loading order, leading to partial imports where some items aren't defined yet. To fix this:
- Move shared code (like base classes) into a third module (e.g.,
game_core.py) that bothintro.pyandmy_character.pycan import from. - Or rearrange your code so one module doesn't depend on the other during initialization.
4. Debug with quick checks
If you're still stuck, add these quick debug lines to confirm what's going on:
- In
my_character.py, add this at the bottom to verify your items are defined:
Runif __name__ == "__main__": # Print all items in the module to confirm they exist print("Items in my_character module:", dir())my_character.pydirectly—you should see your armor/weapon names in the output. - In
intro.py, right after importing, print the module's contents:
If your armor/weapon items aren't in this list, that means they either aren't defined inimport my_character print("Imported my_character contents:", dir(my_character))my_character.pyor the module wasn't imported correctly.
Once you sort out these points, your import should work smoothly, and you can keep building out that awesome text adventure!
内容的提问来源于stack exchange,提问作者Ender Kid

