【问题标题】:Importing a Python Module inside Custom Django Templatetags在自定义 Django 模板标签中导入 Python 模块
【发布时间】:2011-03-16 20:25:29
【问题描述】:

我在我的 Python Django 安装中使用 virtualenv

这是我的目录结构

project/
    dev_environ/
        lib/
            python2.6/
                site-packages/
                    ...
                    django/
                    titlecase/   # <-- The titlecase module
                    PIL/
                    ...
        bin/
            ...
            python  # <-- Python
            ...
        include/

    django_project/
        localsite/
            templatetags/
                __init__.py
                smarttitle.py    # <-- My templatetag module
        foo_app/
        bar_app/
        settings.py
        manage.py

如果我启动我的 Django shell 并尝试导入 titlecase 一切都很好,因为 titlecasesys.path dev_environ/lib/python2.6/site-packages/titlecase 中。

$:django_project cwilcox$ ../dev_environ/bin/python manage.py shell
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import titlecase  # <-- No Import Error!
>>> 

我什至可以在我的settings.py 文件中创建import titlecase 而不会出错。

但是,当我尝试在我的模板标签库 smarttitle.py 中使用 import titlecase 时,我得到一个 ImportError

smarttitle.py如下。

from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
from titlecase import titlecase as _to_titlecase

@register.filter
@stringfilter
def smarttitle(value):
    return _to_titlecase(value)
smarttitle.is_safe = True

不仅如此,我什至可以在呈现模板的视图中 import titlecase 尝试 {% load smarttitle %} 并且没有错误。

我的 Django 开发服务器启动于...

../dev_environ/bin/python manage.py runserver

总结:

我可以导入titlecase 模块任何地方,除了在这个模板标签库中,它会抛出一个ImportError!是什么赋予了?!有什么想法吗?


编辑:我尝试先运行source dev_environ/bin/activate 将我的shell env 切换到我的virtualenv,但这没有帮助——我的模板标签模块中仍然出现 ImportError。我已经手动调用了正确的 python 二进制文件。

【问题讨论】:

  • 可能是一个愚蠢的问题,但您正在执行“source dev_environ/bin/activate”或类似的操作?
  • 我不知道source dev_environ/bin/activate 是什么。听起来我错过了一些重要的东西。我想当我安装一些东西时我所要做的就是dev_environ/bin/pip install &lt;module&gt;
  • 运行 'source /bin/activate' 将您的 $PATH 更改为指向 virtualenv 目录 - 而不是您计算机上的全局 python 安装。在终端中,尝试运行“source dev_environ/bin/activate”,然后运行“python manage.py runserver”——它应该都能正常工作吗?
  • @user608 好的,我查了一下 bin/activate 做了什么。看起来这只是修改了你的 shell,我已经直接调用了正确的 bin/python,所以我认为这不是问题(我将进行编辑以使其更清楚)。
  • 我也有同样的问题。

标签: python django django-templates python-import


【解决方案1】:

如 cmets 中所述,您需要在运行开发服务器之前通过执行source bin/activate(或只是. bin/activate)来激活您的 virtualenv,即使您已经在访问正确的 Python 可执行文件。

【讨论】:

  • 正如我最近在底部的编辑中所说,即使我使用source bin/activate 切换到开发环境,也没有任何变化。当我从 Django manage.py shell 中导入时,我仍然没有收到错误,当我从以 manage.py runserver 开头的开发服务器内的模板标签中导入时,我仍然会收到错误。
  • 如果您的 $PATH 没有问题 - 那么它可能与 titlecase 的安装方式有关。我对 PIL 有类似的问题 - 'easy_install PIL' 安装它,但是很糟糕(尽管如果我没记错的话,仍然可以在 shell 中导入) - 'pip install PIL' 就像一个魅力。
【解决方案2】:

我知道这太旧了,但我今天遇到了类似的问题。

问题似乎是应用和模块使用相同的名称,因此当它尝试导入时,可能无法在错误的位置查找所需的模块或功能。

我建议您为 django 应用或模块提供不同的名称

【讨论】:

    【解决方案3】:

    这不是修复,只是为了确定我们正在查看相同的问题/错误:

    如果您将smarttitle.py 中的导入更改为

    from YOURPROJECT.titlecase import titlecase as _to_titlecase
    

    它可以与“runserver”一起使用,但它会在生产中失败(在我的例子中是 uwsgi/nginx)

    【讨论】:

      猜你喜欢
      • 2015-01-20
      • 2018-07-31
      • 2021-07-13
      • 2011-01-02
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      相关资源
      最近更新 更多