1
votes

I am getting an import error when running pytest using VSCODE python extension. I don't get the error when running pytest from the command line.

I have a python 2.7 project with the below setup.

provision/
|-- venv/
|-- requirements.txt
|-- model_config/
|   |-- ifrs9model_config/
|   |-- |-- __init__.py
|   |-- |-- model_config.py
|   |
|   |-- data/
|   |   |-- input.csv
|   | 
|   |-- tests/
|   |   |--conftest.py
|   |   |--test_model_config.py
|   |   
|   |-- setup.py
|   |-- README

I have installed the package ifrs9model_config into the venv. This allows me to import the ifrs9model_config in conftest.py file like this:

from ifrs9model_config import model_config

When I run conftest.py directly it works fine. When I run pytest from command line (after activating the venv), it also works fine. But when I run pytest using the python extension of VSCODE I get the error:

ImportError while loading conftest '/sasdata/Lev3/apps/provision/model_config/tests/conftest.py'.
tests/conftest.py:6: in <module>
    from ifrs9model_config import model_config
E   ImportError: No module named ifrs9model_config

The vscode pytest is running the following command:

python /home/<userid>/.vscode-server-insiders/extensions/ms-python.python-2020.10.332292344/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /sasdata/Lev3/apps/provision/model_config -s --cache-clear tests

Having printed sys.path (mostly from the conftest.py script), I noticed that the pytest rootdir (/sasdata/Lev3/apps/provision/model_config) is not included in the sys.path when the VSCODE runs pytest. However, it is included when I run pytest from command line using this command:

pytest --rootdir /sasdata/Lev3/apps/provision/model_config/ -s --cache-clear tests

The run_adapter.py script does insert some directories into the sys.path value but this doesn't explain why the above is missing.

Now, if I add a line like this into the conftest.py file:

sys.path.append('/sasdata/Lev3/apps/provision/model_config/')

it works in VSCODE.

I'm especially confused by this because I thought that by installing the ifrs9model_config package into the venv using a setup.py and a 'pip install -e .', the module would be available to any python session in the venv?

I've also read that messing with the sys.path is bad form and either way, I'm not particularly keen on adding code to my scripts just to help VSCODE to run.

Why would the sys.path be different when executing pytest from VSCODE versus command line? And what is the best way to resolve my error?

1
You are doing nothing wrong, the editable install is the best way to go. My guess is that VSCode doesn't use the executable from the venv - how did you configure the interpreter in VSCode?hoefling
Thanks. I had the same thought but I double-checked and it is running from the venv. I had configured the interpreter to run the venv executable (We don't even have pytest installed on the server's generic python install). It's very strange because the sys.path is the exact same one for when I run anything in the venv except that it has removed the path of the module.JoeG
For now, I have just added a line to explicitly add the module path (back) into sys.path to get VSCODE testing working. It's slightly preferable to running pytest manually but I'm sure there's a better way.JoeG

1 Answers

0
votes

I have just experienced the same issue on a devcontainer-based Python project, and solved it by configuring which pytest executable is used by the plugin (in .vscode/settings.json):

{
  "python.testing.pytestPath": "/usr/local/bin/pytest"
}