skip to Main Content

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


  1. There are a few things you can try:

    1. You can change the directory from VS Code configs so that it will treat the working directory as /Python/ instead of ./.
    2. You can add the path reference in your test_example_package.py script like from python.example_package import *. (But for this you have to add a blank init.py in your Python directory)
    Login or Signup to reply.
  2. 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:

    workspace/
        .vscode/
            settings.json
        Python/
            .venv/
                ...
            example_package/
                __init__.py
            tests/
               test_example_package.py
        React/
            ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search