2

他の誰かが、コードの単体テストをunittest.pyという名前のファイルに書き込んで、それがNumPyのunittest.pyモジュールと競合していることに気付いたことがありますか?つまり、これをローカルディレクトリのunittest.pyに書き込むと、次のようになります。

if __name__ == "__main__":
    print "pre-import"
    #import numpy
    print "post-import"

次に(ここでは驚きはありません):

% python unittest.py
pre-import
post-import

しかし、私がそうする場合:

if __name__ == "__main__":
    print "pre-import"
    import numpy
    print "post-import"

私は得る:

% python unittest.py
pre-import
Traceback (most recent call last):
  File "unittest.py", line 3, in <module>
    import numpy
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
    import add_newdocs
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
    from numpy.lib import add_newdoc
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
    from type_check import *
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
    import numpy.core.numeric as _nx
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/core/__init__.py", line 40, in <module>
    from numpy.testing import Tester
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/testing/__init__.py", line 8, in <module>
    from unittest import TestCase
ImportError: cannot import name TestCase

その後、iPythonはロードに失敗します:

% ipython
WARNING: IPython History requires SQLite, your history will not be saved
Traceback (most recent call last):
  File "/home/jbbrown/local_bin/python/bin/ipython", line 7, in <module>
    launch_new_instance()
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 402, in launch_new_instance
    app.initialize()
  File "<string>", line 2, in initialize
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/config/application.py", line 84, in catch_config_error
    return method(app, *args, **kwargs)
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 312, in initialize
    self.init_shell()
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/ipapp.py", line 332, in init_shell
    ipython_dir=self.ipython_dir)
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/config/configurable.py", line 318, in instance
    inst = cls(*args, **kwargs)
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/frontend/terminal/interactiveshell.py", line 183, in __init__
    user_module=user_module, custom_exceptions=custom_exceptions
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 478, in __init__
    self.init_reload_doctest()
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 662, in init_reload_doctest
    doctest_reload()
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/IPython/utils/doctestreload.py", line 72, in doctest_reload
    import doctest
  File "/home/jbbrown/local_bin/python/lib/python2.7/doctest.py", line 2118, in <module>
    class DocTestCase(unittest.TestCase):
AttributeError: 'module' object has no attribute 'TestCase'

If you suspect this is an IPython bug, please report it at:
    https://github.com/ipython/ipython/issues
or send an email to the mailing list at ipython-dev@scipy.org

You can print a more detailed traceback right now with "%tb", or use "%debug"
to interactively debug it.

Extra-detailed tracebacks for bug-reporting purposes can be enabled via:
    c.Application.verbose_crash=True

興味深いことに、インポートステートメントを"if __name__"スイートの外に移動すると、たとえば、

print "pre-import"
import numpy
print "post-import"
if __name__ == "__main__":
    pass

私は得る:

% python unittest.py
pre-import
pre-import
post-import
Traceback (most recent call last):
  File "unittest.py", line 2, in <module>
    import numpy
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/__init__.py", line 137, in <module>
    import add_newdocs
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/add_newdocs.py", line 9, in <module>
    from numpy.lib import add_newdoc
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/__init__.py", line 4, in <module>
    from type_check import *
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/lib/type_check.py", line 8, in <module>
    import numpy.core.numeric as _nx
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/core/__init__.py", line 40, in <module>
    from numpy.testing import Tester
  File "/home/jbbrown/local_bin/python/lib/python2.7/site-packages/numpy/testing/__init__.py", line 8, in <module>
    from unittest import TestCase
ImportError: cannot import name TestCase

もちろん、単体テストを含むファイルの名前を変更してこれを回避するのは簡単ですが、他の誰かがこれに遭遇し、単体テストファイルの名前を変更することを含まないエレガントな回避策を考えたのではないでしょうか。

4

1 に答える 1

1

2.7 より新しいバージョンの Python では、デフォルトで絶対インポートに切り替えることができます。

from __future__ import absolute_import

if __name__ == "__main__":
    print "pre-import"
    import numpy
    print "post-import"
于 2012-04-12T11:08:31.753 に答える