Home > Net >  ImportError: cannot import name 'character_name' from 'Race'
ImportError: cannot import name 'character_name' from 'Race'

Time:12-31

I am building a text based game where you input commands and they execute using if/elif/else statements.

My current goal is to make a file during character creating that the program will be able to pull from, however I am getting stuck with this import error and at a bit of a loss after a few hours of googling.

Traceback (most recent call last):
  File "c:\Users\AlexZ\OneDrive\Documents\Heros-of-Norubia\Core-Game.py", line 1, in <module>
    from Main_Menu import main_menu
  File "c:\Users\AlexZ\OneDrive\Documents\Heros-of-Norubia\Main_Menu.py", line 3, in <module>
    from Character_Creation import character_creation
  File "c:\Users\AlexZ\OneDrive\Documents\Heros-of-Norubia\Character_Creation.py", line 1, in <module>
    from Race import character_name 
ImportError: cannot import name 'character_name' from 'Race' (c:\Users\AlexZ\OneDrive\Documents\Heros-of-Norubia\Race.py)

Core-Game.py from Main_Menu import main_menu main_menu()

Main_Menu.py

from Race_Selection import race_info
from Race_Selection import race_choice
from Character_Creation import character_creation
def main_menu():
    print("Hello!! And welcome to Norubia. Please Choose your race.")

    race_info()
    race_choice()
    character_creation()


Race_selection.py

from elf import elfinfo
from dwarf import dwarfinfo
from giant import giantinfo
from goblin import goblininfo
from human import humaninfo
from minotaur import minotaurinfo
from orc import orcinfo
from troll import trollinfo
from vampier import vampireinfo
from millennial_vampire import millennialvampireinfo
#from dwarf import dwarfselect
#from Character_Creation import character_file
from Race import dwarf
def race_info():
    print("You can choose from the following Races below.")
    print("To learn more about the available races type the race name with a '?' after")
    print("\nPress 'q' at any time to quit")

def race_choice():
    print("\nPlease Choose From The Races Below\nDwarf\nElf\nGiant\nGoblin\nHuman\nMinotaur\nOrc\nTroll\nVampire\nMillennial Vampire")
    player_race = input("What is your race? > ")
    if player_race == 'Dwarf?':
        dwarfinfo()
        race_choice()
    if player_race == 'Elf?':
        elfinfo()
        race_choice()
    if player_race == 'Giant?':
        giantinfo()
        race_choice()
    if player_race == 'Goblin?':
        goblininfo()
        race_choice()
    if player_race == 'Human?':
        humaninfo()
        race_choice()
    if player_race == 'Minotaur?':
        minotaurinfo()
        race_choice()
    if player_race == 'Orc?':
        orcinfo()
        race_choice()
    if player_race == 'Troll?':
        trollinfo()
        race_choice()
    if player_race == 'Vampire?':
        vampireinfo()
        race_choice()
    if player_race == 'Millennial Vampire?':
        millennialvampireinfo()
        race_choice()
    if player_race == 'q':
        exit()
    if player_race == 'Dwarf':
        dwarf()
    else:
        print("That is not a valid selection")
        race_choice()



Race.py

def dwarf():
    player_race = 'Dwarf'
    plyer_name = input("What is your name Dwarf? > ")

Character_Creation.py

from Race import character_name 
def character_creation():
    def character_file():
        character_name = input(f"What is your name {player_race.title()}")
        f = open(f"{character_name}.txt", "x")
        f.write(f"Player Name {character_name.title()}")
        f.close()

I was expecting the character_name to import correctly from the Race File

CodePudding user response:

Your Race.py file only defines the dwarf function, not a character_name, so when you try to import character_name from it, you get the ImportError.

Also doesn't seem you defined character_name anywhere but inside the the character_creation function of Character_Creation.py where you are have already imported a character_name function, therefore shadowing your import with this newly defined function for this scope.

  • Related