代码之家  ›  专栏  ›  技术社区  ›  nskalis

moduleNotfoundError:没有名为的模块

  •  0
  • nskalis  · 技术社区  · 6 年前

    conftest.py 我正在加载模块 blueprint.Manifest ,然后加载模块 Head 例如。请参阅描述底部的最小示例。但我得到以下错误:

    $ py.test manifest/test_schema.py 
    Traceback (most recent call last):
      File "/usr/lib/python3.6/site-packages/_pytest/config/__init__.py", line 381, in _getconftestmodules
        return self._path2confmods[path]
    KeyError: local('/home/app/ip-spotlight/code/cicd/lydia/manifest/test_schema.py')
    
    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "/usr/lib/python3.6/site-packages/_pytest/config/__init__.py", line 381, in _getconftestmodules
        return self._path2confmods[path]
    KeyError: local('/home/app/ip-spotlight/code/cicd/lydia/manifest')
    
    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "/usr/lib/python3.6/site-packages/_pytest/config/__init__.py", line 412, in _importconftest
        return self._conftestpath2mod[conftestpath]
    KeyError: local('/home/app/ip-spotlight/code/cicd/lydia/conftest.py')
    
    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "/usr/lib/python3.6/site-packages/_pytest/config/__init__.py", line 418, in _importconftest
        mod = conftestpath.pyimport()
      File "/usr/lib/python3.6/site-packages/py/_path/local.py", line 668, in pyimport
        __import__(modname)
      File "/usr/lib/python3.6/site-packages/_pytest/assertion/rewrite.py", line 296, in load_module
        six.exec_(co, mod.__dict__)
      File "/home/app/ip-spotlight/code/cicd/lydia/conftest.py", line 5, in <module>
        from blueprint.Manifest import ManifestSchema
      File "/home/app/ip-spotlight/code/cicd/lydia/blueprint/Manifest.py", line 5, in <module>
        from Head import HeadSchema
    ModuleNotFoundError: No module named 'Head'
    ERROR: could not load /home/app/ip-spotlight/code/cicd/lydia/conftest.py
    

    我现在的环境是

    $ py.test --version
    This is pytest version 3.8.0, imported from /usr/lib/python3.6/site-packages/pytest.py
    setuptools registered plugins:
      pytest-xdist-1.23.0 at /usr/lib/python3.6/site-packages/xdist/plugin.py
      pytest-xdist-1.23.0 at /usr/lib/python3.6/site-packages/xdist/looponfail.py
      pytest-timeout-1.3.2 at /usr/lib/python3.6/site-packages/pytest_timeout.py
      pytest-sugar-0.9.1 at /usr/lib/python3.6/site-packages/pytest_sugar.py
      pytest-metadata-1.7.0 at /usr/lib/python3.6/site-packages/pytest_metadata/plugin.py
      pytest-html-1.19.0 at /usr/lib/python3.6/site-packages/pytest_html/plugin.py
      pytest-forked-0.2 at /usr/lib/python3.6/site-packages/pytest_forked/__init__.py
      celery-4.2.1 at /usr/lib/python3.6/site-packages/celery/contrib/pytest.py
    

    最小示例:我的项目结构如下:

    $ tree
    .
    |-- attributes
    |   `-- __init__.py
    |-- bgp
    |   `-- __init__.py
    |-- blueprint
    |   |-- Head.py
    |   |-- __init__.py
    |   |-- InterfacesIx.py
    |   |-- InterfacesLacp.py
    |   |-- Manifest.py
    |   |-- NeighborIx.py
    |   `-- Tail.py
    |-- communities
    |   `-- __init__.py
    |-- conftest.py
    |-- customize
    |   `-- __init__.py
    |-- interfaces
    |   `-- __init__.py
    |-- manifest
    |   |-- blueprint
    |   |-- __init__.py
    |   `-- test_schema.py
    |-- policies
    |   `-- __init__.py
    |-- prefixes
    |   `-- __init__.py
    `-- pytest.ini
    

    这个 混杂的事物 如下所示:

    import os
    import yaml
    import pytest
    
    from blueprint.Manifest import ManifestSchema
    
    
    @pytest.fixture(scope="session")
    def manifest():
        with open(
            "{}/ip-spotlight/code/cicd/manifest.yaml".format(os.environ["APPFOLDER"]), "rt"
        ) as f:
            manifest = yaml.safe_load(f.read())
            schema = ManifestSchema()
            appobj = schema.load(manifest)
            return appobj
    

    blueprint/Manifest.py 如下所示:

    import marshmallow
    import dictdiffer
    import requests
    
    from Head import HeadSchema
    

    请你告诉我做错了什么,并帮助我解决这个问题好吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   phd    6 年前
    from Head import HeadSchema
    

    这是绝对导入,意味着包是针对python 2的。

    最后一次发布是在 2013 . 这个包裹太旧了。