Python3 - How do I import a class from another file

R.Wieser address at not.available
Mon Dec 9 03:15:10 EST 2019


Greg,

>>> Are you sure there's a difference between classes and functions here?
>>
>> Yes, quite sure.
>
> And we're even more sure that there isn't. :-)

As it turned out "we're" are right, and I was thrown off by the error 
message. :-p

The problem is that I had no idea why I got that (rather explicit) error 
message, and took it by its face-value - sending me of in the wrong 
direction.

The actual caise ?  I made a mistake by, in the testcode (which I had not 
blocked off) in the imported file creating an instance with the exact same 
name as the class.   The deleting of that instance at the end of the 
testcode somehow caused the class to disappear as well, resulting in the 
"can't find it" error.

To be honest, I was not aware/did not assume that I could delete a class 
definition that way (I even find it a bit weird, but as I'm a newbie in 
regard to Python that does not say much).

Bottom line: I was caught by my own choice to give the instance the exact 
same name as the class. :-\

>You'll find that *any* form of import executes all the top-level code.

Thats certainly something I did not expect.

Regards,
Rudy Wieser




More information about the Python-list mailing list