私を怒らせる可能性のあるImportErrorがあります。状況は次のようになります。
tests/
testWebsite.py
website/
__init__.py
__main__.py
_webtools/
__init__.py
templatedefs.py
...
_templates/
base.mako
article.mako
...
コード(問題が解決する前にコミットすることを躊躇するテストディレクトリはありません)は、https ://github.com/Boldewyn/website/からオンラインで入手できます。
を呼び出すpython -m website.__main__ build
と、メインルーチンは、の下のテンプレートを使用して、いくつかの入力静的HTMLファイルから作成しますwebsite/_templates
。これは、任意のディレクトリで問題なく機能します。
ただし、tests/testWebsite.py
私には単体テストがあり、それも同じことを実行する必要があります。ただし、Makoテンプレートでは、ファイルのインポートエラーが発生します。他の場合は正常にインポートされます。
$ head -n 5 website/_templates/article.mako
# -*- coding: utf-8 -*-
<%!
from website._webtools.templatedefs import strip_tags
%>
<%inherit file="base.mako" />
テストを実行すると、次のようになります。
$ python -m unittest tests.testWebsite
...
ERROR: test_initial_build (tests.testWebsite.BuildTestCase)
Check, if building directly after bootstrap works
----------------------------------------------------------------------
Traceback (most recent call last):
File "tests/testWebsite.py", line 99, in test_initial_build
File "website/_webtools/build.py", line 89, in build
article.save(articles=articles)
File "website/_webtools/articles.py", line 514, in save
template_engine.render_article(self, **ctx)
File "website/_webtools/templates.py", line 52, in render_article
r.render_article(article, **ctx)
File "website/_webtools/templates.py", line 277, in render_article
tpl = self.lookup.get_template(filename)
File "/usr/lib/python2.7/dist-packages/mako/lookup.py", line 217, in get_template
return self._load(srcfile, uri)
File "/usr/lib/python2.7/dist-packages/mako/lookup.py", line 277, in _load
**self.template_args)
File "/usr/lib/python2.7/dist-packages/mako/template.py", line 205, in __init__
module = self._compile_from_file(path, filename)
File "/usr/lib/python2.7/dist-packages/mako/template.py", line 249, in _compile_from_file
filename)
File "/usr/lib/python2.7/dist-packages/mako/template.py", line 470, in _compile_text
exec code in module.__dict__, module.__dict__
File "_templates_article_mako", line 16, in <module>
ImportError: No module named templatedefs
面白いのは、sys.path
テンプレートから直接印刷できることです。
<%!
import sys
print sys.path
from website._webtools.templatedefs import strip_tags
%>
そして、私はそこで確認することができます、それwebsite
は道の中にあります。また、インポートは他のすべての展開シナリオでうまく機能します。
インポートwebsite
するかwebsite._webtools
、うまく機能します。一部だけwebsite._webtools.templatedefs
がうまくいかない。
何がうまくいかない可能性があるかの兆候を見つけるためにどこを見ることができるか、誰かアイデアがありますか?
テストコードは非常に単純です。
class BuildTestCase(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
self.cwd = os.getcwd()
os.chdir(self.tmpdir)
bootstrap(self.tmpdir, { # this initiates a new project
"URL": "localhost",
"TITLE": "Example",
"DEFAULTS": {
"AUTHOR": "John Doe",
}
})
def test_initial_build(self):
"""Check, if building directly after bootstrap works"""
build()
def tearDown(self):
os.chdir(self.cwd)
shutil.rmtree(self.tmpdir)
編集:もう1つの診断:makoにテンプレートをコンパイルさせ、結果のPythonファイルをスタンドアロンで実行しました。チャームのように機能します。また、templatedefs.pyを最小限に減らし(defのみが空の文字列を返す)、そのファイルのImportErrors(またはその他の奇妙な点)も除外できるようにしました。
システム情報: Ubuntu 11.04、Python 2.7、Mako0.3.6。