本文介绍了pyramid_jinja2:ImportError:无法导入名称环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关Pyramid框架的教程,而我目前仍停留在教您使用Jinja2作为模板引擎..看来,当我使用nosetests运行测试时,出现了这个奇怪的ImportError,表明无法从Jinja2模块导入名称Environment.有趣的是,当我pserve该站点时,它可以正常工作.看来此错误只会在测试期间发生...我目前正在使用Ubuntu 14.04来处理virtualenv.

I'm going through the tutorial for the Pyramid framework and I'm currently stuck at the one that teaches the use of Jinja2 as the templating engine.. It seems that when I run the test using nosetests, I get this odd ImportError stating that the name Environment couldn't be imported from the Jinja2 module. Funny thing is, when I pserve the site, it works just fine. It seems like this error will only occur during the test...I am currently working off a virtualenv using Ubuntu 14.04.

有什么建议吗?

这是堆栈跟踪...

======================================================================
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

----------------------------------------------------------------------

推荐答案

@Michael Merickel是正确的.您的jinja2模块与实际模块之间存在冲突.这个错误源于python2的导入策略.但是您可以在脚本魔术句子from __future__ import absolute_import的第一行中添加.它将相对导入更改为绝对导入.

@Michael Merickel is right. You have a conflict between your jinja2 module and real one. This mistake grows from python2 import politics. But you can add to first line of your script magic sentence from __future__ import absolute_import. It changes relative import to absolute.

更多信息: PEP 0328-导入:多行和绝对/相对

但是,如果您想从/home/bk/pg/pyramid/quick_tutorial/执行脚本,我的解决方案将无济于事.因为在这种情况下,有两种可能的方法可以解决import jinja2.

But my solution doesn't help you if you want to execute your script from /home/bk/pg/pyramid/quick_tutorial/. Because in this situation where will be two possible way to resolve import jinja2.

这篇关于pyramid_jinja2:ImportError:无法导入名称环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 15:46