Py3: Import relative path module

Peter Otten __peter__ at
Wed Nov 3 09:51:46 CET 2010

MRAB wrote:

> On 03/11/2010 01:48, Gnarlodious wrote:
>> I admit I don't understand any of what was said here. Or why it is so
>> hard what I am trying to do. I searched Google for a few hours looking
>> for a way to import a module from an absolute path, but that was a
>> waste of time too.
>> To reiterate, I am trying to import a Def function from a file one
>> level up. Let's say relative import is unreasonable to expect from
>> Python. If it is possible using an absolute path, please let me know
>> how, in plain language. Otherwise I can continue to copy and paste my
>> programming into several files.
>> Thank you for any help.
> After some experimentation (and Googling!) I think the problem is that
> a module can do your relative import but a main script can't.
> Or, to put it another way, if __name__ is "__main__" then it won't work.

Slightly generalized: have the importing module print its __name__. There 
has to be at least one dot in the name for

from .. import whatever

to succeed.

More information about the Python-list mailing list