So lösen Sie inkompatible Importe in Python und PytestPython

Python-Programme
Guest
 So lösen Sie inkompatible Importe in Python und Pytest

Post by Guest »

Ich habe ein Projekt erstellt, das genau wie die Dokumentation der PyTest Good Integration Practices beschrieben wurde, insbesondere die Tests außerhalb des Anwendungscodes. >

Code: Select all

.
├── pyproject.toml
├── src
│   └── basic_package
│       ├── bar.py
│       ├── __init__.py
│       └── main.py
└── tests
└── test_app.py

In main.py importiere ich aus bar.py

Code: Select all

from bar import baz

def main() -> str:
return baz()
In test_app.py

Code: Select all

from basic_package.main import main

def test_foo():
assert main() == 'qux'
Es funktioniert, wenn ich das Projekt ausführe. Wenn ich jedoch pytest ausführe, erhalte ich eine Fehlermeldung

ModuleNotFoundError: Kein Modul namens „bar“
Es funktioniert in Pytest, wenn ich den Code in main.py in
änderefrom .bar import baz
< /code>
Aber wenn ich die Anwendung ausführe, erhalte ich den ModulenotFoundError < /p>
Wie kann ich dieses Problem beheben? < /p>
Ich leite pyTest aus dem Stammverzeichnis des Projekts

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post