这几天一直在玩弄 djangoproject.com 的源码,对里边的那个RSS Aggregator很感兴趣,一直想知道它的实现原理,结果是crontab每小时执行一次 @.@ 在Windows下要跑起来似乎对于我这种菜鸟有点困难,折腾了几天终于摸清楚了这个 update_feeds.py 的玩法。
简介
- Django是一个免费的开源Python Web框架
- djangoproject.com 是Django的官方网站,网站源码可以在 http://code.djangoproject.com/svn/djangoproject.com/下载到。
- djangoproject.com源码里有一个Community栏目,是一个RSS Aggregator,可以同步多个RSS源,每小时更新一次。
- djangoproject.com用的是PostgreSQL,我把它修改成了Python 2.5默认支持的SQLite3,数据库放在
E:\Python\reference\djangoprojectwebsite\django_website\djangoproject
- 我把SVN Checkout的源码放在
E:\Python\reference\djangoprojectwebsite,那个自动更新RSS聚合的python在 E:\Python\reference\djangoprojectwebsite\django_website\apps\aggregator\bin\update_feeds.py
No module named django_website.apps.aggregator.models
起初没弄成功,出现的错误提示是:
Traceback (most recent call last):
File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 57, in
update_feeds()
File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 13, in update_feeds
From django_website.apps.aggregator.models import Feed, FeedItem
ImportError: No module named django_website.apps.aggregator.models
这个错误的原因是没有在系统环境变量里定义 PYTHONPATH 指向当前工程的路径。
Environment variable DJANGO_SETTINGS_MODULE is undefined.
Traceback (most recent call last):
File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 57, in
update_feeds()
File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 13, in update_feeds
From django_website.apps.aggregator.models import Feed, FeedItem
File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\..\\django_websit
e\\apps\\aggregator\\models.py", line 1, in
From django.db import models
File "C:\\Python25\\Lib\\site-packages\\django\\db\\__init__.py", line 7, in
if not settings.DATABASE_ENGINE:
File "C:\\Python25\\Lib\\site-packages\\django\\conf\\__init__.py", line 28, in __ge
tattr__
self._import_settings()
File "C:\\Python25\\Lib\\site-packages\\django\\conf\\__init__.py", line 53, in _imp
ort_settings
raise EnvironmentError, "Environment variable %s is undefined." % ENVIRONMEN
T_VARIABLE
EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.
这里需要定义 DJANGO_SETTINGS_MODULE 系统环境变量,也可以通过 update_feeds.py --settings=django_website.settings 来指定。
no such table: aggregator_feeds
Traceback (most recent call last):
File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 57, in
update_feeds()
File "E:\\Python\\reference\\djangoprojectwebsite\\django_website\\apps\\aggregator\\
bin\\update_feeds.py", line 14, in update_feeds
for feed in Feed.objects.filter(is_defunct=False):
File "C:\\Python25\\Lib\\site-packages\\django\\db\\models\\query.py", line 108, in _
_iter__
return iter(self._get_data())
File "C:\\Python25\\Lib\\site-packages\\django\\db\\models\\query.py", line 470, in _
get_data
self._result_cache = list(self.iterator())
File "C:\\Python25\\Lib\\site-packages\\django\\db\\models\\query.py", line 183, in i
terator
cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join
(select) + sql, params)
File "C:\\Python25\\Lib\\site-packages\\django\\db\\backends\\util.py", line 12, in e
xecute
return self.cursor.execute(sql, params)
File "C:\\Python25\\Lib\\site-packages\\django\\db\\backends\\sqlite3\\base.py", line
93, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: aggregator_feeds
这个错误困扰我时间最长。最后发现是当前路径的问题:不能直接到 bin 目录去执行 update_feeds.py,需要到数据库所在的路径,通过相对路径来执行 update_feeds.py 才能成功执行。
正确执行 update_feeds.py 的步骤
- 首先在cmd里用cd命令切换到django_website目录,也就是djangoproject.com的源码跟目录
E:\>cd /d E:\Python\reference\djangoprojectwebsite\django_website
- 然后设置 PYTHONPATH 和 DJANGO_SETTINGS_MODULE 环境变量
E:\Python\reference\djangoprojectwebsite\django_website>set PYTHONPATH=E:\Python\reference\djangoprojectwebsite
E:\Python\reference\djangoprojectwebsite\django_website>set DJANGO_SETTINGS_MODULE=django_website.settings
- 最后执行 update_feeds.py,注意用相对路径启动 update_feeds.py
E:\Python\reference\djangoprojectwebsite\django_website>apps\aggregator\bin\update_feeds.py
终于开始更新了,嘎嘎