Python / Django正在导入错误的模块(相对应该是绝对的) [英] Python/Django is importing the wrong module (relative when it should be absolute)

查看:480
本文介绍了Python / Django正在导入错误的模块(相对应该是绝对的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Django 1.2 pre-alpha和Python 2.4。是的,我知道,但我坚持下去。我们目前无法升级,我怀疑这是答案。



我有两个模板标签库, foo bar 。但是, foo 也是顶级包的名称,它恰好是 bar 的包:

  foo-1.2.3 / 
foo /
conf /
settings.py
templatetags /
bar.py

bar-4.5 /
somepackage /
templatetags /
foo.py

标签库 bar.py 包含一行如下:

 从foo.conf导入设置

...你会期望它加载 foo-1.2.3 / foo / conf / settings.py



但不是:


TemplateSyntaxError:'bar'不是有效的标签库:无法加载模板库从django.templatetags.bar,没有模块命名为conf


不幸的是,Django执行一点魔法,并绑定所有模板标签lib更新到 django.templatetags。* 。因此, bar 正在导入为 django.templatetags.bar ,当它从 foo.conf导入设置它最终导入 bar-4.5 / somepackage / templatetags / foo.py 。呃!



你有什么想法吗?



我在导入之前设置了一个断点,我已经确认 foo-1.2.3 sys.path ,但 import 关键字仍然发现错误的 foo



如果有帮助,请注意,我可以修改 foo-1.2.3 软件包(因为它已经在本地检查并正在逐步淘汰)但是我拒绝修改 bar-4.5 软件包(因为它是一个开放源码的软件包,并已被系统安装)。

解决方案

经过几个小时的黑客攻击,这是一个伎俩。



原始代码:

 从foo.conf导入设置

新代码

  foo = __import __('foo')
conf = __import __('foo .conf')。conf
settings = __import __('foo.conf.settings')。conf .settings

(我可能不需要第二行。)



Ewww。


I'm using Django 1.2 pre-alpha and Python 2.4. Yeah, I know, but I'm stuck with it. We can't upgrade at the moment and I doubt that's the answer anyway.

I've got two template tag libraries, foo and bar. However, foo is also the name of a top-level package, and it happens to be the package of bar:

foo-1.2.3/
  foo/
    conf/
      settings.py
    templatetags/
      bar.py

bar-4.5/
  somepackage/
    templatetags/
      foo.py

The tag library bar.py contains a line like this:

from foo.conf import settings

...and you would expect it to load foo-1.2.3/foo/conf/settings.py.

But no:

TemplateSyntaxError: 'bar' is not a valid tag library: Could not load template library from django.templatetags.bar, No module named conf

Unfortunately, Django performs a little magic and binds all template tag libraries to django.templatetags.*. Thus, bar is being imported as django.templatetags.bar, and when it calls from foo.conf import settings it ends up importing bar-4.5/somepackage/templatetags/foo.py. Ugh!

Do you have any ideas how to fix this?

I've set a breakpoint right before the import, and I've confirmed that foo-1.2.3 is at the beginning of sys.path, but the import keyword still finds the wrong foo.

If it helps, note that I can modify the foo-1.2.3 package (because it's been checked in locally and is being phased out), but I refuse to modify the bar-4.5 package (because it's an open-source package and has been installed system-wide).

解决方案

After a few more hours of hacking, this did the trick.

Original code:

from foo.conf import settings

New code:

foo = __import__('foo')
conf = __import__('foo.conf').conf
settings = __import__('foo.conf.settings').conf.settings

(I probably don't need the second line.)

Ewww.

这篇关于Python / Django正在导入错误的模块(相对应该是绝对的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆