[Tutor] Relative import help

Matthew Ngaha chigga101 at gmail.com
Sat Sep 22 12:56:12 CEST 2012

i have an assignment from a book to practice implementing  relative
imports. it gave a very clear and easy to follow explanation, but my
results are proving the instructions are wrong. here's what i have
tried. my folder structure:

Project /

   Domestic /

      Europe /

         Internationals /


its easy to use the normal import method to import something further
down the directory, but when trying to go up, nothing works. heres
something weird. winner.py in the Europe directory, i used this code
to try and import a class named Silverware in silverware.py 1
directory up:

from ..silverware import Silverware
heres my error mesages: ...  ValueError: Attempted relative import in

i can accept this and say i did something wrong, but.... i left the
import statement even with the error. now my main.py in the 1st
directory called Project. i imported winner.py. the file that got this
error message. And by importing this file, i have access to
silverware.py. i can access everything in this file through that old
import statement:

from ..silverware import Silverware
so why is it this statement wont allow me to import silverware.py to
winner.py directly, but still gives access to winner.py if winner.py
is imported from a directory on the same level or further up from
silverware.py? this is the exact code instructions my book gave me,
why isn't it working for files futher down?

More information about the Tutor mailing list