Matthew Ngaha wrote: > 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 / > > __init__.py > main.py > Domestic / > > __init__.py > players.py > premier_leauge.py > silverware.py > Europe / > > __init__.py > champions_league.py > europa.py > winners.py > Internationals / > > __init__.py > nations.py > > 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 > non-package > > 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?
You probably have a path that reaches into Domestic or Europe sub-package. That can happen if e. g. Project/Domestic/Europe is your current working directory. Try to cd into the Project folder's parent directory and see if $ python -c 'import Project.Domestic.Europe.winners' works. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor