py.test:ImportError - 无法导入----

时间:2016-02-24 12:36:05

标签: python virtualenv importerror

我正在使用其他人的代码,可在GitHub上找到。为了运行他们的代码,我创建了一个virtualenv并安装了所有列出的依赖项 - python库和其他存储库的克隆。当我继续运行包含的测试时,我得到ImportError

    Namespace(all=False, regr=False, sci=False, unit=True)
    [localhost] local: py.test -x -v engine/test
    ==================================================================================== test session starts =====================================================================================
    platform linux2 -- Python 2.7.6, pytest-2.8.2, py-1.4.31, pluggy-0.3.1 -- /home/compomics/local/METASPACE/SM_distributed/SM_engine/bin/python
    cachedir: engine/test/.cache
    rootdir: /home/compomics/local/METASPACE/SM_distributed/engine/test, inifile: 
    collecting 6 items / 1 errors
    =========================================================================================== ERRORS ===========================================================================================
    _______________________________________________________________________ ERROR collecting test_formula_img_validator.py _______________________________________________________________________
    engine/test/test_formula_img_validator.py:7: in <module>
        from engine.formula_img_validator import filter_sf_images,get_compute_img_measures, ImgMeasures
    engine/formula_img_validator.py:7: in <module>
        from pyIMS.image_measures import measure_of_chaos, isotope_image_correlation, isotope_pattern_match
    E   ImportError: cannot import name measure_of_chaos
    !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    ================================================================================== 1 error in 0.99 seconds ===================================================================================

    Fatal error: local() encountered an error (return code 2) while executing 'py.test -x -v engine/test'

但是,如果我打开python解释器并尝试执行完全相同的导入,它就可以正常运行而不会出现任何错误。类似的问题建议:

  • 将空__init__.py添加到测试目录
  • 确保在virtualenv
  • 中安装pytest

我做了这两件事,错误仍然存​​在。

我添加到测试脚本的开头:

    import os
    print(os.environ["PYTHONPATH"].split(os.pathsep))
    print(os.listdir("."))

并确认我尝试导入的文件夹确实在结果列表中。

不确定如何继续。非常感谢我能得到的任何帮助:)

2 个答案:

答案 0 :(得分:0)

与其他图书馆存在冲突

编辑 - 这是我自己的愚蠢而不记得我已经克隆了以前版本的依赖回购,这也是我的路径,并且不包括此代码试图加载的功能。很抱歉,当我注意到没有删除问题时,我无法为我的生活找到删除按钮:)

答案 1 :(得分:0)

在文件formula_img_validator.py中更改

from pyIMS.image_measures import measure_of_chaos,isotope_image_correlation, isotope_pattern_match

from engine.pyIMS.image_measures import measure_of_chaos, isotope_image_correlation, isotope_pattern_match

那将解决问题。要获得完整的解决方案go to GitHub for new updated code