You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.py is in the exact same folder as intro.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.py instead of my_character.py are 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.py has:
    # 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:
    1. 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()
      
    2. 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()
      

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 both intro.py and my_character.py can 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:
    if __name__ == "__main__":
        # Print all items in the module to confirm they exist
        print("Items in my_character module:", dir())
    
    Run my_character.py directly—you should see your armor/weapon names in the output.
  • In intro.py, right after importing, print the module's contents:
    import my_character
    print("Imported my_character contents:", dir(my_character))
    
    If your armor/weapon items aren't in this list, that means they either aren't defined in my_character.py or 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:19:42