简体   繁体   English

在Django模板标签库中导入外部库时出错

[英]Error importing external library within Django template tag library

So I'm attempting to write a Django reusable app that provides a method for displaying your Twitter feed on your page. 因此,我试图编写一个Django可重用应用程序,该应用程序提供了一种在页面上显示Twitter feed的方法。 I know well that it already exists 20 times. 我知道它已经存在20次了。 It's an academic exercise. 这是一项学术活动。 :) :)

Directory structure is pretty simple: 目录结构非常简单:

myproject
|__  __init__.py
|__  manage.py
|__  settings.py
|__  myapp
     |__  __init__.py
     |__  admin.py
     |__  conf
          |__  __init__.py
          |__  appsettings.py
     |__  feedparser.py
     |__  models.py
     |__  templates
          |__  __init__.py
     |__  templatetags
          |__  __init__.py
          |__  twitterfeed.py
     |__  views.py
|__  templates
          |__  base.html
|__  urls.py

When running the Django shell, the functions defined in twitterfeed.py work perfectly. 运行Django Shell时,twitterfeed.py中定义的功能可以完美运行。 I also believe that I have the template tags properly named and registered. 我也相信我已经正确命名和注册了模板标签。

As you can see, I use the excellent Universal Feed Parser . 如您所见,我使用了出色的Universal Feed Parser My problem is not within UFP itself, but in UFP's inability to be called while importing the template tag library. 我的问题不在UFP本身内,而是在导入模板标签库时无法调用UFP。 When I {% load twitterfeed %} in base.py, I get the following error: 当我在base.py中{% load twitterfeed %}时,出现以下错误:

'twitterfeed' is not a valid tag library: Could not load template library from django.templatetags.twitterfeed, No module named feedparser 'twitterfeed'不是有效的标记库:无法从django.templatetags.twitterfeed加载模板库,没有名为feedparser的模块

I import feedparser using the following statement: 我使用以下语句导入feedparser:

import re, datetime, time, myapp.feedparser

The best I can tell, this error message is slightly deceiving. 我所能告诉的最好的是,此错误消息有些欺骗。 I think there's an ImportError going on when the template library is loaded, and this is Django's interpretation of it. 我认为加载模板库时会发生ImportError,这是Django对它的解释。

Is there any way I can import feedparser.py within my reusable app without requiring users of the app to place feedparser somewhere in their PythonPath? 有什么方法可以在我的可重用应用程序中导入feedparser.py,而无需该应用程序的用户将feedparser放置在其PythonPath中的某个位置?

Thanks! 谢谢!

I solve this kind of problem (shipping libraries that are dependencies for my overall project) in the following way. 我通过以下方式解决了此类问题(运送作为整个项目依赖项的库)。 First, I create an "ext" directory in the root of my project (in your case that would be myproject/ext ). 首先,我在项目的根目录中创建一个“ ext”目录(在您的情况下为myproject/ext )。 Then I place dependencies such as feedparser in that ext directory - myproject/ext/feedparser 然后,将诸如feedparser之类的依赖项放在该ext目录中myproject/ext/feedparser

Finally, I change my manage.py script to insert the ext/ directory at the front of sys.path. 最后,更改我的manage.py脚本,将ext /目录插入sys.path的前面。 This means both ./manage.py runserver and ./manage.py shell will pick up the correct path: 这意味着./manage.py runserver./manage.py shell都将选择正确的路径:

# manage.py
import os, sys
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'ext'))
# ... rest of manage.py

I find this works really well if you don't want to mess around with things like virtualenvs. 如果您不想弄乱virtualenvs之类的东西,我发现这确实很好用。 When you deploy your project you have to make sure the path is correct as well - I usually solve this by adding the same sys.path.insert line to the start of my mod_wsgi app.wsgi file. 部署项目时,还必须确保路径正确-我通常通过在mod_wsgi app.wsgi文件的开头添加相同的sys.path.insert行来解决此问题。

This looks like one of those annoying relative path issues - solved in Python 2.6 and higher (where you can do import ..feedparser etc) but often a bit tricky on older versions. 这看起来像是那些烦人的相对路径问题之一-在Python 2.6及更高版本中已解决(您可以在其中导入..feedparser等),但在较旧的版本上通常会有些棘手。 One cheap and cheerful way to fix this could be just to move feedparser.py in to your templatetags directory, as a sibling to twitterfeed.py 解决此问题的一种便宜而愉快的方法可能是将feedparser.py移到templatetags目录中,作为twitterfeed.py的同级兄弟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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