Repository navigation
Pytest doesn't respect PYTHONPATH in .env #6915
Description
Environment data
- VS Code version: 1.37.0, 1.37.0-insider
- Extension version (available under the Extensions sidebar): 2019.6.22090, 2019.6.24221, 2019.8.29288
- OS and version: CentOS 7.5.1804, macOS 10.14.6
- Python version (& distribution if applicable, e.g. Anaconda): 3.6.5, 3.6.8
- Type of virtual environment used (N/A | venv | virtualenv | conda | ...): pyenv-virtualenv
- Relevant/affected Python packages and their versions: pytest 5.0.1
- Jedi or Language Server? (i.e. what is
"python.jediEnabled"set to; more info How to update the language server to the latest stable version #3977): LanguageServer
Expected behaviour
Test discovery and running have enviroment variables (including PYTHONPATH) declared in the .env file
Actual behaviour
Tests that depend on a specific environment variable being set to run fail.
Steps to reproduce:
- Create a new workspace folder (in my case: /home/singleheart/mywork)
- Add a new folder "mypackage" in the workspace folder
- Add a new python file under the mypackage folder named "mymodule.py" as follows:
def foo():
pass
- Add a new file under the tests folder named test_one.py as follows:
from mypackage.mymodule import foo
def test_case_one():
foo()
-
Add new line in .env file (/home/singleheart/mywork is the location of my workspace)
PYTHONPATH=/home/singleheart/mywork -
Create a settings file .vscode/settings.json with the following contents:
{
"python.testing.pytestArgs": ["tests"],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true
}
Logs
Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)
> ~/.pyenv/versions/convlab/bin/python ~/.vscode-server-insiders/extensions/ms-python.python-2019.8.29288/pythonFiles/testing_tools/run_adapter.py discover pytest -- -s --cache-clear tests
cwd: ~/work/test
> ~/.pyenv/versions/convlab/bin/python -c "import pytest"
> ~/.pyenv/versions/convlab/bin/python -c "import pytest
Output for Python Test Log in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python Test Log)
==================================== ERRORS ====================================
______________________ ERROR collecting tests/test_one.py ______________________
ImportError while importing test module '/home/singleheart/mywork/tests/test_one.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
tests/test_one.py:2: in <module>
from mypackage.mymodule import foo
E ModuleNotFoundError: No module named 'mypackage'
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.10 seconds ============================
Traceback (most recent call last):
File "/home/singleheart/.vscode-server-insiders/extensions/ms-python.python-2019.8.29288/pythonFiles/testing_tools/run_adapter.py", line 18, in <module>
main(tool, cmd, subargs, toolargs)
File "/home/singleheart/.vscode-server-insiders/extensions/ms-python.python-2019.8.29288/pythonFiles/testing_tools/adapter/__main__.py", line 90, in main
parents, result = run(toolargs, **subargs)
File "/home/singleheart/.vscode-server-insiders/extensions/ms-python.python-2019.8.29288/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 36, in discover
raise Exception('pytest discovery failed (exit code {})'.format(ec))
Exception: pytest discovery failed (exit code 2)
Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)
Error Python Extension: 2019-08-09 14:51:28: Failed to parse discovered Test [Error: ============================= test session starts ==============================platform linux -- Python 3.6.5, pytest-4.3.1, py-1.8.0, pluggy-0.9.0rootdir: /home/singleheart/mywork, inifile:plugins: flaky-3.5.3collected 0 items / 1 errors==================================== ERRORS ====================================______________________ ERROR collecting tests/test_one.py ______________________ImportError while importing test module '/home/singleheart/mywork/tests/test_one.py'.Hint: make sure your test modules/packages have valid Python names.Traceback:tests/test_one.py:1: in <module> from mypackage.mymodule import fooE ModuleNotFoundError: No module named 'mypackage'===Flaky Test Report======End Flaky Test Report===!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!=========================== 1 error in 0.11 seconds ============================Traceback (most recent call last): File "/home/singleheart/.vscode-server-insiders/extensions/ms-python.python-2019.8.29288/pythonFiles/testing_tools/run_adapter.py", line 18, in <module> main(tool, cmd, subargs, toolargs) File "/home/singleheart/.vscode-server-insiders/extensions/ms-python.python-2019.8.29288/pythonFiles/testing_tools/adapter/__main__.py", line 90, in main parents, result = run(toolargs, **subargs) File "/home/singleheart/.vscode-server-insiders/extensions/ms-python.python-2019.8.29288/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 36, in discover raise Exception('pytest discovery failed (exit code {})'.format(ec))Exception: pytest discovery failed (exit code 2) at ChildProcess.a.once (/home/singleheart/.vscode-server-insiders/extensions/ms-python.python-2019.8.29288/out/client/extension.js:9:37624) at Object.onceWrapper (events.js:273:13) at ChildProcess.emit (events.js:182:13) at maybeClose (internal/child_process.js:962:16) at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)]
Activity
karrtikr commentedon Aug 14, 2019
PYTHONPATHaccepts relative paths to the workspace. So please try usingPYTHONPATH=.and reload the window.singleheart commentedon Aug 19, 2019
It resolves my problem. Thank you!