简体   繁体   中英

Django couldn't find my module if it is imported from a templetetag

I'm upgrading an existing Django project to v1.7 and creating some templatetags to simplify working with templates. I've created some before, but one of them is giving the following error:

ImportError raised loading recaptcha.templatetags.recaptcha: No module named utils .

This module looks like this:

- recaptcha
     |- templatetags
     |         |- __init__.py
     |         |- recaptcha.py
     |
     |- __init__.py
     |- utils.py

The problematic import is in recaptcha.py and I tried from recaptcha import utils , from recaptcha.utils import myfunction and import recaptcha but none of them works. If I only import recaptcha and I try to execute dir on it, it shows nothing.

The weird thing is that if I import the utils module inside another django app in a view.py or a model.py file it works perfectly. Also I tried using manage.py shell and it works too.

As a clarification, this project does not use virtualenv right now.

Any ideas? Thank you!

The solution that @catavaran answered before works: using a relative import like from ..utils import myfunction

The problem was that the templatetag module recaptcha.py and the django app recaptcha had both the same name , so when I type from recaptcha import utils it tries to find it from itself instead of from the recaptcha module . I renamed the django app to catpcha and it imports the module without problem.

@catavaran I don't really know why your answer is deleted, but if you post it again I'll mark it as the right one, you deserve the kudos!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM