简体   繁体   中英

Pytest cannot find a module in a different folder

My Python project structure is

my_project
  src
    extensions
      my_tool.py
  tests
    main_test.py
  pytest.ini
  .env

In main_test.py I'm importing a class which is defined in my_tool.py like:

from extensions import main_tool as tool

hammer = tool.Hammer()

I'm using Pipenv. I followed the these steps:

  • Activated my env by running pipenv shell
  • Installed pytest. pipenv install -d pytest

I even added pytest.ini file which has this content

[pytest]
pythonpath = src/extensions

The file .env has

PYTHONPATH=${PYTHONPATH}:src/extensions

I cannot get this to work. Whenever I run pytest from the root of my project I always get

tests/main_test:1: in <module>
    from extensions import main_tool as tool
E   ModuleNotFoundError: No module named 'extensions'

You just need to point pythonpath to src as you are using a from extensions import... :

Change pytest.ini to:

[pytest]
pythonpath = src/

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