2

私を怒らせる可能性のある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。

4

1 に答える 1

3

これは確かに1人の狂気を駆り立てます。ただし、ここにいくつかのことがあります。

  1. ./nosetests:これは機能し、9つのテストすべてに合格します

  2. 'templatedefs'makoテンプレートにを追加して比較するとき'_webtools.__dict__'に欠落している唯一のキーです:他の部分はすでに以前にインポートされています'from website import _webtools''nosetests''python -m unittest tests.testWebsite'

  3. sys.pathケースには(相対パス)が含まれますが、絶対パスのみが含まれるケース''には含まれません。これにより、次の値が異なります。1つは相対的、もう1つは絶対的です。を作成すると、相対パスは機能しなくなります。'python -m unittest tests.testWebsite''nosetests'sys.path'website._webtools.__file__'['website/_webtools']['/home/username/tmp/website/_webtools']os.chdir

SO:純粋なunittestを使用する場合'import website._webtools.templatedefs'は、テストファイルの先頭に追加できます。これにより、を実行するときにtemplatedefが確実にインポートされますos.chdir。そして、私は鼻を使用することをお勧めします。お役に立てば幸いです。

于 2011-08-06T16:57:41.310 に答える