Reputation: 9937
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
Upvotes: 3
Views: 1946
Reputation: 29312
Works fine with me.
With the following project structure:
project/
|-- run.py
`-- utils
| |-- __init__.py
| |-- utils.py
|-- backup
|-- __init__.py
|-- utils.py
Where you call python3 run.py
from the project
directory.
Edit: Sorry I just saw that this does not apply to python-2.x
.
Upvotes: 0
Reputation: 29747
You may use relative imports:
from ..utils.utils import cool_function
Upvotes: 2
Reputation: 14091
If project1
is a package (parent dir is on sys.path
and has an __init__.py
), you can do from 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_function
may also work.
Upvotes: 1