pyramid_jinja2: ImportError: cannot import name Environment

[亡魂溺海] 提交于 2019-12-12 01:58:23

问题


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.

Any suggestions?

Here's the stack trace...

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

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

回答1:


@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.

More information: PEP 0328 -- Imports: Multi-Line and Absolute/Relative

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.




回答2:


It's hard to say but my first guess is that it looks like you named your project jinja2 and that's going to conflict with the jinja2 package in some weird ways depending on what module is doing the imports etc.



来源:https://stackoverflow.com/questions/31271419/pyramid-jinja2-importerror-cannot-import-name-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!