如果从模板标签导入,Django找不到我的模块

时间:2022-10-07 23:14:43

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:

我正在将现有的Django项目升级到v1.7并创建一些模板标签以简化模板的使用。我以前创建了一些,但其中一个给出了以下错误:

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

ImportError引发加载recaptcha.templatetags.recaptcha:没有名为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.

有问题的导入是在recaptcha.py中,我尝试从recaptcha import utils,从recaptcha.utils导入myfunction和import recaptcha,但没有一个工作。如果我只导入recaptcha并尝试在其上执行dir,则不显示任何内容。

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.

奇怪的是,如果我在view.py或model.py文件中的另一个django应用程序中导入utils模块,它可以很好地工作。我也尝试使用manage.py shell,它也可以。

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

作为澄清,该项目目前不使用virtualenv。

Any ideas? Thank you!

有任何想法吗?谢谢!

1 个解决方案

#1


1  

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

@catavaran之前解决的解决方案有效:使用来自..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.

问题是templatetag模块recaptcha.py和django app recaptcha都有相同的名称,所以当我从recaptcha import utils中输入时,它会尝试从自身而不是recaptcha模块中找到它。我将django应用程序重命名为catpcha,它可以毫无问题地导入模块。

@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!

@catavaran我真的不知道为什么你的答案会被删除,但是如果你再次发布它我会把它标记为正确的答案,你值得称赞!

#1


1  

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

@catavaran之前解决的解决方案有效:使用来自..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.

问题是templatetag模块recaptcha.py和django app recaptcha都有相同的名称,所以当我从recaptcha import utils中输入时,它会尝试从自身而不是recaptcha模块中找到它。我将django应用程序重命名为catpcha,它可以毫无问题地导入模块。

@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!

@catavaran我真的不知道为什么你的答案会被删除,但是如果你再次发布它我会把它标记为正确的答案,你值得称赞!