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?