The Python Zen says:
There should be one-- and preferably only one --obvious way to do it
What's the right way to import another file from the same folder in Python 2.7
.
└── myfolder
├── file1.py
├── file2.py
└── __init__.py
(Assuming __init__.py
is empty)
Solution 1:
file2.py
should contain
from myfolder import file1
this works fine: python -m myfolder/file2
However, this approach requires naming myfolder
everywhere (which seems silly: you shouldn't have to refer to your family members by their last names and cities of origin)
Solution 2:
On the other hand, this fails with "ValueError: Attempted relative import in non-package":
from . import file1
Solution 3:
as does this:
from __future__ import absolute_import
from . import file1
Solution 4:
And this version fails with "No module named file1":
import file1
Solution 5:
Putting every file in its own folder with __init__.py
next to it also doesn't seem like a very sane approach.