1

Running pytest in the terminal results in the below error. The application runs fine with python3 main.py from the src/ directory, however pytest throws this error.

____________________________________________________________________________________ ERROR collecting tests/test_with_pytest.py ____________________________________________________________________________________
    ImportError while importing test module '/Users/Lucas/Library/Mobile Documents/com~apple~CloudDocs/Documents/tests/test_with_pytest.py'.
    Hint: make sure your test modules/packages have valid Python names.
    Traceback:
    /usr/local/Cellar/[email protected]/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py:126: in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
    tests/test_with_pytest.py:1: in <module>
        from src import csv_methods
    src/csv_methods.py:2: in <module>
        from prompts import *
    E   ModuleNotFoundError: No module named 'prompts'

I am using pipenv for the virtual env.

Project structure:

├── Pipfile
├── Pipfile.lock
├── README.md
├── __pycache__
│   └── ...
├── requirements.txt
├── src
│   ├── __init__.py
│   ├── __pycache__
│   │   └── ...
│   ├── app_objects.md
│   ├── asset.py
│   ├── command_line.py
│   ├── csv_data
│   │   └── assets.csv
│   ├── csv_methods.py
│   ├── input.py
│   ├── log.log
│   ├── main.py
│   ├── menu.py
│   └── prompts.py
└── tests
    ├── __init__.py
    └── test_with_pytest.py

I have tried the popular answers and common results such as adding __init__.py files within the test, src folder and the route directory. Note the init.py files are blank, I have tried the recommendations from the following posts such as adding in here here and here

I saw the solution about adding PYTHONPATH=. pytest to the project but its unclear in which file etc to add this.

Also tried running with python3 -m pytest but no luck.

python3 -m site

sys.path = [
    '/Users/Lucas/Library/Mobile Documents/com~apple~CloudDocs/Documents/',
    '/usr/local/Cellar/[email protected]/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python311.zip',
    '/usr/local/Cellar/[email protected]/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11',
    '/usr/local/Cellar/[email protected]/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload',
    '/Users/Lucas/.local/share/virtualenvs/asset_management_system-GiLvwx5p/lib/python3.11/site-packages',
]
USER_BASE: '/Users/Lucas/Library/Python/3.11' (doesn't exist)
USER_SITE: '/Users/Lucas/Library/Python/3.11/lib/python/site-packages' (doesn't exist)
ENABLE_USER_SITE: False

css_methods.py

#  Functions to perform read, write to csv data stores
from prompts import *
import csv
import os
from tabulate import tabulate
import logging

---snip---

test_with_pytest.py

from src import csv_methods
from src import prompts

def test_always_passes():
    assert True

Pipfile.lock

"_meta": {
        "hash": {
            "sha256": "d2f84cfd3b709ff81d112ff5862ef667b82358c63d00cdf29b529b0535b4c08a"
        },
        "pipfile-spec": 6,
        "requires": {
            "python_version": "3.11"
        },
        "sources": [
            {
                "name": "pypi",
                "url": "https://pypi.org/simple",
                "verify_ssl": true
            }
        ]

python --version Python 3.11.5

1 Answer 1

0

You need to decided whether src is a single package or if it's just a collection of individual modules.

In test_with_pytest, you are using from src import ..., meaning you are treating src as a package. For this you need an __init__.py file.

When running python main.py, and when doing imports like import prompts, you are treating it as if it's just a collection of single-file modules.

You probably should treated it as a single package (and rename the folder something more descriptive/unique than src). Then you would always, in all files, use from src import ... or from src.prompts import ..., and would run it with python -m src.main.

Sign up to request clarification or add additional context in comments.

1 Comment

This was really help I used from src.prompts import and created a pyproject.toml file with [tool.pytest.ini_options] pythonpath = [ ".", "src", ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.