Suppose I have the following (project1 is on python path):
/project1/utils/utils.py
def cool_function():
print "cool"
/project1/backup/utils.py
from utils.utils import cool_function
This throws “ImportError: No module named utils”.
I assume this is because it is searching for utils.cool_function in backup.utils. Is there a rather than renaming the utils package? I think my naming convention makes sense, and is natural, so I’m reluctant to changing it. If that however is preferred and standard practice, I will rename it!
Thanks!
EDIT: I am using Python 2.7
If
project1is a package (parent dir is onsys.pathand has an__init__.py), you can dofrom project1.utils.utils import cool_function. See also PEP328, which is new in python 2.5. If you’re using 2.5 or later,from ..utils.utils import cool_functionmay also work.