简体   繁体   中英

Pytest ImportError: No module named

I have been looking for different issues and seems that pytest is having a issue with relative path.

Anywhere I am looking a solution is either to add the path of the project when importing modules on the test classes or running pytest as a module with python -m pytest

In Both cases im I am having the same issue with "no module named

python -m pytest 
======================================================================================== test session starts =========================================================================================
platform linux2 -- Python 2.7.16, pytest-3.10.1, py-1.7.0, pluggy-0.8.0
rootdir: /home/patrick/PycharmProjects/ScrapperController, inifile:
collected 0 items / 1 errors                                                                                                                                                                         

=============================================================================================== ERRORS ===============================================================================================
___________________________________________________________________________ ERROR collecting app/tests/bank_model_test.py ____________________________________________________________________________
ImportError while importing test module '/home/patrick/PycharmProjects/ScrapperController/app/tests/bank_model_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python2.7/dist-packages/six.py:709: in exec_
    exec("""exec _code_ in _globs_, _locs_""")
app/tests/bank_model_test.py:2: in <module>
    from app.bank_xpath import BankXpath
E   ImportError: No module named app.bank_xpath
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
====================================================================================== 1 error in 0.07 seconds =======================================================================================

The structure of my project is

project
project/app
project/app/bank_xpath.py
project/app/controller.py
project/tests
project/tests/bank_model_tests.py

This is a partial code how I am importing modules in the class.

import unittest
from app.bank_xpath import BankXpath
from app.dictionary import ERRORS


class TestingBanksModels(unittest.TestCase):

    def test_bank_wrong_iso(self):
        bankObject = create_correct_bank()
        bankObject.fromCurrency = "Danish Krones"
        error = [ERRORS["wrong_from_currency"]]
        self.assertEqual(bankObject.validate(), error)

If you're using python2, I believe you'll need a file named __init__.py (it can be empty) in the app/ directory in order for the import to work. I don't think this is an issue in python3.

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