How do I resolve this ImportError? [solved]

Hi all.

Being the dork I am, I’m in the process of trying to create a Zelda-themed text RPG. I’ve been going through the process of just getting the basic classes – items, characters – coded up. I also created a small Die class that represents an n-sided die for probability stuff. The code for that class is here:

I also have my item class that currently is basic stuff for items – weapons, healing, etc. That code is here:

My tests were working fine until I added the import statement, which I’m going to use later. The interpreter gave me “ImportError: cannot import name ‘roll’”. I looked into it and saw some people on StackOverflow getting it, but they were having circular dependency issues. I doubt that’s the case here – only imports from the random module. That said, what’s going on? Also, my code is all in one directory. For now, anyways.

The short answer is that you are trying to import a method and not a function, or class, or whatever. You could just import the whole class. When Python tries to import the stuff you need, the roll method is not detected because is whitin the class. The roll method belongs only to “class Item”. Eventually, I think you will need to use some advanced Object-Oriented concepts to achieve what you are trying to do.

I hope this will help you for now.

1 Like

Hey @TriforceHero777, @funception has the right idea, but I’ll add something.

Remember that your python file is kind of a directory or a dict. Inside is the class Die, and class Die is also a dict. So you have has class Die which has function run.

Ok, you wanted to get to a directory fish that had blue that had truck in it, you’d do something like this:

cd fish/blue/truck

In your code above, if you treated it like a directory it’d be kind of like this:

cd Die/roll

But, in Python you use a dot (.) to do that, and don’t have to say “.py” and “class”:

cd die.Die.roll

And we don’t do cd we do something like this:


Ok, what you basically did was like trying to get to truck in our directory example above by doing this:

cd fish/truck

Truck isn’t inside fish, it’s inside blue, so you can’t do that. You get an error. When you do this:

from die import roll

You’re telling Python to do:


But roll is not inside die, it’s in die.Die. What you need to do is:

from die import Die

Then you use the Die class. NOW, this also means you don’t quite understand what a class does and how that becomes an object, so I suggest you study that more as well. What it looks like is you’re treating classes like modules, and either you need to use them to make objects, or just use modules.

Bleh. That’s such a simple thing to fix. I blame the fact that yesterday was unseasonably warm. My head was probably just fogged over from the heat. It really shouldn’t be 90 in early May in Massachusetts…stupid climate change. Thanks for the help.

I should tell you about the bug that took me a week to solve and was just a single character in a single file.

Was it Python? If it was, I would hardly be surprised – Python syntax errors are quite vague.

No, Java, and a single character in a randomly misspelled word in a tiny XML file ruined me.

1 Like