ModuleNotFoundError:未命名模块

时间:2018-09-18 12:33:37

标签: python-3.x pytest

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

conftest.py看起来像这样:

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 个答案:

答案 0 :(得分:0)

from Head import HeadSchema

这是绝对导入,这意味着该软件包适用于Python 2。

最新版本位于2013中。包装太旧了。