I have an existing Python project which looks like this:
.venv/
...
example_package/
__init__.py
tests/
test_example_package.py
Imagine that I worked on this project for a while and then decided to add a frontend to some kind of web service using React.
I created a new directory called React
, and a new directory called Python
to organize my code.
The project structure now looks like this:
Python/
.venv/
...
example_package/
__init__.py
tests/
test_example_package.py
React/
...
But there is a problem. My tests no longer work. So I ran the VS Code "configure tests" command and set the subdirectory where the tests are to be found to Python
. But this did not fix the whole issue, and the pytest
discovery process fails with the following error:
ModuleNotFoundError: No module named 'example_package'
It seems relatively obvious as to what is happening. pytest
is being run by VS Code, with a current working directory set to .
instead of ./Python/
.
How can I fix this?
2
Answers
There are a few things you can try:
VS Code
configs so that it will treat the working directory as/Python/
instead of./
.test_example_package.py
script likefrom python.example_package import *
. (But for this you have to add a blank init.py in yourPython
directory)Your project file is already at the same level as the vscode workspace, and the Settings in the workspace will not take effect for the parent folder. I recommend that you select a new workspace.
The new directory structure is more like: