【问题标题】:Trying to import a class from another file but my test breaks试图从另一个文件导入一个类,但我的测试中断
【发布时间】:2020-12-01 16:07:32
【问题描述】:

我正在尝试使用 prettyconf - https://github.com/osantana/prettyconf - 来使用 .env 文件。

我创建了一个 config.py 文件并将其放在我的脚本的同一个文件夹中。

我的 config.py 是这个:

from prettyconf import config


class Settings:
    ENVIRONMENT = config(
        'ENVIRONMENT',
        default='dev',
        cast=config.option({'dev': 'dev', 'int': 'int', 'prod': 'prod'}),
    )

    LOG_LEVEL = config('LOG_LEVEL', default='INFO')
 

settings = Settings()

在我的脚本中,我以这种方式导入我的 config.py:

from cl_uploader.config import settings

但是我收到了这个错误消息:

Traceback (most recent call last):
  File "cl_uploader.py", line 7, in <module>
    from cl_uploader.config import settings
  File "/home/myfolder/Doing/folder/cl_uploader/cl_uploader.py", line 7, in <module>
    from cl_uploader.config import settings
ModuleNotFoundError: No module named 'cl_uploader.config'; 'cl_uploader' is not a package

我尝试改成这样的相对路径:

from .config import settings

但是我收到了这个错误:

Traceback (most recent call last):
  File "cl_uploader.py", line 7, in <module>
    from .config import settings
ImportError: attempted relative import with no known parent package

但如果是这样的让:

from config import settings

有效!但是……

我的测试开始中断,我收到了这个消息:

____________________________ ERROR collecting tests/test_cl_uploader.py _____________________________
ImportError while importing test module '/home/myfolder/Doing/folder/tests/test_cl_uploader.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
../../.cache/pypoetry/virtualenvs/cl-uploader-12nYBdPj-py3.8/lib/python3.8/site-packages/_pytest/python.py:511: in _importtestmodule
    mod = self.fspath.pyimport(ensuresyspath=importmode)
../../.cache/pypoetry/virtualenvs/cl-uploader-12nYBdPj-py3.8/lib/python3.8/site-packages/py/_path/local.py:704: in pyimport
    __import__(modname)
../../.cache/pypoetry/virtualenvs/cl-uploader-12nYBdPj-py3.8/lib/python3.8/site-packages/_pytest/assertion/rewrite.py:152: in exec_module
    exec(co, module.__dict__)
tests/test_cl_uploader.py:10: in <module>
    from cl_uploader.cl_uploader import check_stack_exists
cl_uploader/cl_uploader.py:7: in <module>
    from config import settings
E   ModuleNotFoundError: No module named 'config'
====================================== short test summary info ======================================
ERROR tests/test_cl_uploader.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================= 1 error in 0.25s ==========================================

我的树文件夹是这样设置的:

.
├── cl_uploader
│   ├── cl_uploader.py
│   ├── config.py
│   ├── __init__.py
│   └── resources
│       └── teste.yaml
├── file.tmp
├── local.env
├── Makefile
├── poetry.lock
├── pyproject.toml
├── README.md
└── tests
    ├── __init__.py
    └── test_cl_uploader.py

【问题讨论】:

  • 这能回答你的问题吗? Using pytest with a src layer
  • 您的项目没有 src 层,但无论如何修复都会起作用 - 如果您正在编写看起来像包的东西,请使其可安装。它将解决任何导入错误,而无需求助于黑客 sys.path

标签: python python-3.x pytest python-poetry


【解决方案1】:

您已经在cl_uploader 文件夹中。试试:from config import settings

__init__.py 中添加:

import os, sys
sys.path.append(os.path.dirname(os.path.realpath(__file__)))

然后,此目录中的所有文件都将位于sys.path。可能需要将文件名更改为 cl_config.py 以避免冲突。

【讨论】:

    【解决方案2】:

    您的包名为cl_uploader,并且您在此包中有一个模块cl_uploader。避免这种名称重复。

    默认情况下,如果python能找到模块或包,它会先在当前工作目录中查找,然后再去其他地方。

    因此,当您在 cl_uploader 文件夹中时,它将拾取 cl_uploader 模块而不是包。如果您在外面,它会取走包裹。

    总而言之:将cl_uploader.py 重命名为其他名称,一切都会正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-19
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多