【问题标题】:ModuleNotFoundError: No module namedModuleNotFoundError:没有命名的模块
【发布时间】:2018-09-19 01:02:39
【问题描述】:

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

您能否告知我做错了什么并帮助我解决这个问题?

【问题讨论】:

  • 我刚刚发现我错了,我虽然你的 pytest 模块是你的文件之一,但事实证明有一个名为 pytest 的模块,你的问题可能是由其他原因引起的
  • 你可以在blueprint/Manifest.py里面试试from blueprint.Head import HeadSchema

标签: python-3.x pytest


【解决方案1】:
from Head import HeadSchema

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

最后一个版本是在2013。包太旧了。

【讨论】:

    猜你喜欢
    • 2021-09-10
    • 2020-08-15
    • 2019-03-28
    • 2019-05-31
    • 2022-01-07
    • 2021-09-14
    • 2017-12-14
    相关资源
    最近更新 更多