简体   繁体   中英

Python: ImportError: attempted relative import with no known parent package

I have a very simple python project:

sample/__init__.py
sample/main.py
tests/test_sample.py

main.py contains:

def echo_the_arg(the_arg):
    return the_arg

test_sample.py contains:

import unittest
from sample.main import echo_the_arg

class SampleTest (unittest.TestCase):
    def test_echo_the_arg(self):
        assert echo_the_arg(1) == 1

My IDE resolves sample.main module OK:

在此处输入图像描述

but when I attempt to run the test from the root of the project by issuing pytest tests I get an error:

ImportError while importing test module '/Users/me/python-project-template/tests/test_sample.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/Cellar/python@3.9/3.9.2_4/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_sample.py:2: in <module>
    from sample.main import echo_the_arg
E   ModuleNotFoundError: No module named 'sample'

I've perused the many many search results on SO pertaining to relative imports like this but I'm none the wiser as to why i'm getting this failure. Why can pytest not find my sample module?


UPDATE, I discovered I can run my tests by running python -m pytest tests

platform darwin -- Python 3.9.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/me/python-project-template
collected 1 item                                                                                                                                                                                          

tests/test_sample.py .                                                                                                                                                                              [100%]

======================= 1 passed in 0.02s =======================

so I'm left wondering why just running pytest from the command-line fails.

I figured it out. I needed to add an extra (empty) __init__.py file. So my structure now looks like this:

sample/__init__.py
sample/main.py
tests/__init__.py
tests/test_sample.py

Output:

(venv) ➜  python-project-template git:(master) ✗ pytest tests          
=========================================================================================== test session starts ===========================================================================================
platform darwin -- Python 3.9.2, pytest-6.2.4, py-1.10.0, pluggy-0.13.1
rootdir: /Users/me/python-project-template
collected 1 item                                                                                                                                                                                          

tests/test_sample.py .                                                                                                                                                                              [100%]

======================= 1 passed in 0.02s =======================

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM