2012-04-12 6 views
2

Hat jemand anderes die Situation, wo sie Komponententests für ihren Code in eine Datei namens unittest.py geschrieben haben, festgestellt, und es einen Konflikt mit Numpty unittest.py gefunden Modul? Mit anderen Worten, wenn ich das schreibe in einem lokalen Verzeichnis unittest.py:Python: Vermeidung von Konflikt von NumPy unittest.py mit lokalen unittest.py

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

Then (keine Überraschungen hier):

% python unittest.py 
pre-import 
post-import 

Aber wenn ich tue:

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

Ich bekomme:

% 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 

Anschließend kann iPython nicht geladen werden :

% 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 [email protected] 

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 

Interessanterweise, wenn ich die Import-Anweisung außerhalb der "if __name__" Suite bewegen, zum Beispiel

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

ich:

% 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 

Natürlich den Namen ändern es trivial ist der Datei, die meine Komponententests enthält, und vermeide dies, aber ich frage mich, ob irgendjemand anderen dies angetan hat und an eine elegante Problemumgehung gedacht hat, die das Umbenennen ihrer Komponententestdatei nicht einschließt?

Antwort

1

Sie können neuere auf Python-Versionen absolute imports standardmäßig wechseln als 2,7:

from __future__ import absolute_import 

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