Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pytest doesn't respect PYTHONPATH in .env #6915

Closed
singleheart opened this issue Aug 9, 2019 · 2 comments
Closed

Pytest doesn't respect PYTHONPATH in .env #6915

singleheart opened this issue Aug 9, 2019 · 2 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster

Comments

@singleheart
Copy link

singleheart commented Aug 9, 2019

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 (ViewOutput, 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 (ViewOutput, 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)]
@singleheart singleheart added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Aug 9, 2019
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Aug 12, 2019
@karrtikr karrtikr added the info-needed Issue requires more information from poster label Aug 14, 2019
@karrtikr
Copy link

karrtikr commented Aug 14, 2019

PYTHONPATH accepts relative paths to the workspace. So please try using PYTHONPATH=. and reload the window.

@singleheart
Copy link
Author

PYTHONPATH accepts relative paths to the workspace. So please try using PYTHONPATH=. and reload the window.

It resolves my problem. Thank you!

@ghost ghost removed the triage label Aug 19, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Aug 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster
Projects
None yet
Development

No branches or pull requests

3 participants