我正在阅读金字塔框架的教程,我目前仍然坚持teaches the use of Jinja2 as the templating engine.。似乎当我使用nosetests
运行测试时,我得到了这个奇怪的ImportError,指出无法从Jinja2模块导入名称Environment。有趣的是,当我pserve
网站时,它运作得很好。看起来这个错误只会在测试期间发生......我目前正在使用Ubuntu 14.04处理virtualenv。
有什么建议吗?
这是堆栈跟踪......
======================================================================
ERROR: test_home (jinja2.tutorial.tests.TutorialFunctionalTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/bk/pg/pyramid/quick_tutorial/jinja2/tutorial/tests.py", line 30, in setUp
app = main({})
File "/home/bk/pg/pyramid/quick_tutorial/jinja2/tutorial/__init__.py", line 5, in main
config.include('pyramid_jinja2')
File "/home/bk/pg/pyramid/venv/local/lib/python2.7/site-packages/pyramid/config/__init__.py", line 727, in include
c = self.maybe_dotted(callable)
File "/home/bk/pg/pyramid/venv/local/lib/python2.7/site-packages/pyramid/config/__init__.py", line 825, in maybe_dotted
return self.name_resolver.maybe_resolve(dotted)
File "/home/bk/pg/pyramid/venv/local/lib/python2.7/site-packages/pyramid/path.py", line 320, in maybe_resolve
return self._resolve(dotted, package)
File "/home/bk/pg/pyramid/venv/local/lib/python2.7/site-packages/pyramid/path.py", line 327, in _resolve
return self._zope_dottedname_style(dotted, package)
File "/home/bk/pg/pyramid/venv/local/lib/python2.7/site-packages/pyramid/path.py", line 376, in _zope_dottedname_style
found = __import__(used)
File "/home/bk/pg/pyramid/venv/local/lib/python2.7/site-packages/pyramid_jinja2-2.5-py2.7.egg/pyramid_jinja2/__init__.py", line 6, in <module>
from jinja2 import Environment as _Jinja2Environment
ImportError: cannot import name Environment
----------------------------------------------------------------------
答案 0 :(得分:3)
jinja2
模块与真实模块之间存在冲突。这个错误来自python2导入政治。但是你可以添加你的脚本魔术句from __future__ import absolute_import
的第一行。它将相对导入更改为绝对。
更多信息:PEP 0328 -- Imports: Multi-Line and Absolute/Relative
但是,如果您想从/home/bk/pg/pyramid/quick_tutorial/
执行脚本,我的解决方案无法帮助您。因为在这种情况下将有两种可能的方法来解决import jinja2
。
答案 1 :(得分:1)
很难说,但我的第一个猜测是,您看起来像是将项目命名为jinja2
,并且这会以某种奇怪的方式与jinja2
包冲突,具体取决于执行导入的模块等