You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
pytest fails module not found, even tho tests are discovered & works in debug, #15398
Thank you for your help! I have search many issues, and tried many solutions, but I can't figure it out. I understand this is OSS, and no one may have time or interest to help me. That's okay. I am very grateful for vscode-python, and appreciate your time.
use python 3.6 to create a virtualenv: $py -3.6 -m venv venv
install requirements for pvlib and install pvlib in editable mode: pip install --no-deps -e .
open pvlib folder in vscode, it chooses venv automatically, yay!
see test are all discovered
try to run tests, select pytest as test runner, use default setup.cfg as only setting
pick any test and run it, but it fails to import pvlib in conftest.py
Details
ImportError while loading conftest 'c:\Users\mikm\Projects\pvlib-python\pvlib\tests\conftest.py'.
pvlib\tests\conftest.py:10: in <module>
import pvlib
E ModuleNotFoundError: No module named 'pvlib'
Error: TypeError: Cannot read property 'testsuites' of null
run the same test again using debug, and it works!
try it in the console or a windows CMD, and it works! (venv) $ pytest pvlib/tests/test_infinite_sheds.py::test_gcr_prime
Does this issue occur when all extensions are disabled?: Yes/No
If I disable extensions, then the Python extension isn't loaded, and so no tests are discovered, and so I can't run them.
I have the following extensions installed and enabled: Python, Remote-WSL, Jupyter, Git History, C#, and C/C++
If I disable everything but Python and Jupyter (I cannot disable only Jupyter), the tests are still discovered, but still do not run.
FYI: I've tried the latest insider's build of vscode and the python extension and set "python.useIsolation": false in my .vscode/settings.json and the same error still occurs.
However, if I re-install pvlib using python setup.py install now it works. So I cannot tell if this is a duplicate of #14570 or not. My guess is that it is a duplicate, but then I don't understand why using the insider's version breaks it.
I'd also like to know how others are using pytest and virtualenvs in vscode?
Mark Mikofski (@mikofski), thanks for letting us know about this. Please provide the content of the "Python" output panel and the "Python Test Log" output panel. Also, please provide the content of your settings.json file. Of particular relevance is the "python.testing.pytestArgs" setting.
Another setting to try is "python.testing.cwd". Set it to the root directory from which you want to run your tests. Normally this is the workspace root.
Finally, what happens when you run pytest manually in a terminal? What about if you run the same command that shows up in the "Python" output panel? The command output will be helpful.
ImportError while loading conftest 'c:\Users\mikm\Projects\pvlib-python\pvlib\tests\conftest.py'.
pvlib\tests\conftest.py:10: in <module>
import pvlib
E ModuleNotFoundError: No module named 'pvlib'
Error: TypeError: Cannot read property 'testsuites' of null
I think my settings.json file is like this, it was auto generated:
Pytest works fine when I run it manually in a terminal or from the vscode Python output. And discover tests works and tests run in debug. It's only the green play button on the GUI that raises the exception.
I'm away from my laptop, but I will try setting python.testing.cwd asap. Thanks!
Mark Mikofski (@mikofski) I see the same behavior on my mac in my conda environment and having installed pvlib using pip install -e .. It's been awhile since I've used vscode in this way so I didn't notice when it broke (for no particular reason I am in the habit of using vscode's debug test or running from the command line). I'll guess it worked about 3 months ago. Sorry I didn't actually try this when you first reported the issue - I initially understood it to be limited to virtualenv.
The relationship to installation issues and a desire to avoid real work led me try installing the package with pyproject.toml and moving the pytest config to that file too. Same issue. Branch here.
FYI: I've tried the latest insider's build of vscode and the python extension and set "python.useIsolation": false in my .vscode/settings.json and the same error still occurs.
I believe you need to set "python.useIsolation": false in your User Settings not your Workspace settings in VS Code Insiders. #14570 (comment)
Will Holmgren (@wholmgren) FYI vscode testing is working in other projects, so there is something specific about pvlib that is different.
conftest.py
tests/ doesn't have __init__.py
tests/ isn't at the project level, it's inside the pvlib package
Have you tried the vscode insiders build and setting "python.useIsolation": false in the User Settings? This supposedly fixes the issue, but either I did it wrong or it didn't work for me. I guess setting python.useIsolation to false undoes this line:
I guess I'm not terribly concerned b/c pytest works in a terminal and that's good enough for me. And visual debug still works in code, so that's nice. I'm kinda hoping this isn't a larger issue, and it just works itself out in time. 😏
FYI: I've tried the latest insider's build of vscode and the python extension and set "python.useIsolation": false in my .vscode/settings.json and the same error still occurs.
I believe you need to set "python.useIsolation": false in your User Settings not your Workspace settings in VS Code Insiders. #14570 (comment)
That worked for me.
I would like to add that this also solved my bug.
This being that pytest or in other words testing does not respect PYTHONPATH when it is in the root directory.
The fix, that is, adding "python.useIsolation": false` to Preferences: Open Settings (JSON) solved the following issue:
create a parent directory example_parent
create a directory named foo and at the same level create a directory called test
within the foo directory create a main.py file containing the following code:
def return_4():
return 4
within the test directory create a file called test_file.py containing the following code:
from foo.main import return_4
def test_4():
assert return_4() != 4
add the parent directory that foo and test reside in to your PYTHONPATH (with Mac this is done through sudo nano /private/etc/zprofile and then add : export PYTHONPATH=~/folder/to/example_parent
open vscode
within vscode open example_parent
press ctrl+shift+p and then type python all tests
enable and configure pytest in the root directory
run pytest within vscode through the python run all tests setting with vscode opened in root
You will receive the following error
python /Users/francesco/.vscode/extensions/ms-python.python-2021.4.765268190/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir /Users/francesco/Work/example_parent -s --cache-clear .
Test Discovery failed:
Error: ============================= test session starts ==============================
platform darwin -- Python 3.8.2, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
rootdir: /Users/francesco/Work/example_parent
plugins: xdist-2.2.1, forked-1.3.0
collected 0 items / 1 error
==================================== ERRORS ====================================
_____________________ ERROR collecting tests/test_file.py ______________________
ImportError while importing test module '/Users/francesco/Work/example_parent/tests/test_file.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/importlib/__init__.py:127: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
tests/test_file.py:2: in <module>
from foo.main import return_4
E ModuleNotFoundError: No module named 'foo'
=========================== short test summary info ============================
ERROR tests/test_file.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
===================== no tests collected, 1 error in 0.08s =====================
Traceback (most recent call last):
File "/Users/francesco/.vscode/extensions/ms-python.python-2021.4.765268190/pythonFiles/testing_tools/run_adapter.py", line 22, in <module>
main(tool, cmd, subargs, toolargs)
File "/Users/francesco/.vscode/extensions/ms-python.python-2021.4.765268190/pythonFiles/testing_tools/adapter/__main__.py", line 100, in main
parents, result = run(toolargs, **subargs)
File "/Users/francesco/.vscode/extensions/ms-python.python-2021.4.765268190/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 44, in discover
raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 2)
Activity
mikofski commentedon Feb 15, 2021
FYI: I've tried the latest insider's build of vscode and the python extension and set
"python.useIsolation": falsein my.vscode/settings.jsonand the same error still occurs.However, if I re-install pvlib using
python setup.py installnow it works. So I cannot tell if this is a duplicate of #14570 or not. My guess is that it is a duplicate, but then I don't understand why using the insider's version breaks it.I'd also like to know how others are using pytest and virtualenvs in vscode?
ericsnowcurrently commentedon Feb 17, 2021
Mark Mikofski (@mikofski), thanks for letting us know about this. Please provide the content of the "Python" output panel and the "Python Test Log" output panel. Also, please provide the content of your settings.json file. Of particular relevance is the
"python.testing.pytestArgs"setting.Another setting to try is "python.testing.cwd". Set it to the root directory from which you want to run your tests. Normally this is the workspace root.
Finally, what happens when you run pytest manually in a terminal? What about if you run the same command that shows up in the "Python" output panel? The command output will be helpful.
mikofski commentedon Feb 17, 2021
Thank you very much!!!
Here is the Python test log output:
I think my
settings.jsonfile is like this, it was auto generated:Pytest works fine when I run it manually in a terminal or from the vscode Python output. And discover tests works and tests run in debug. It's only the green play button on the GUI that raises the exception.
I'm away from my laptop, but I will try setting
python.testing.cwdasap. Thanks!5 remaining items
wholmgren commentedon Feb 23, 2021
Mark Mikofski (@mikofski) I see the same behavior on my mac in my conda environment and having installed pvlib using
pip install -e .. It's been awhile since I've used vscode in this way so I didn't notice when it broke (for no particular reason I am in the habit of using vscode's debug test or running from the command line). I'll guess it worked about 3 months ago. Sorry I didn't actually try this when you first reported the issue - I initially understood it to be limited tovirtualenv.wholmgren commentedon Feb 23, 2021
The relationship to installation issues and a desire to avoid real work led me try installing the package with pyproject.toml and moving the pytest config to that file too. Same issue. Branch here.
mikofski commentedon Feb 25, 2021
Will Holmgren (@wholmgren) I found a sneaky but unpleasant workaround. I insert this into
pvlib/tests/conftest.pyright beforeimport pvlib:If I open a python interpreter and print the Python path the outcome of the "editable" install is that
pvlib-pythonis last in the list:but printing out the Python path from inside
pvlib/tests/conftest.py:shows during test execution it's been removed:
mhostetter commentedon Mar 4, 2021
I believe you need to set
"python.useIsolation": falsein your User Settings not your Workspace settings in VS Code Insiders. #14570 (comment)That worked for me.
mikofski commentedon Mar 31, 2021
Will Holmgren (@wholmgren) FYI vscode testing is working in other projects, so there is something specific about pvlib that is different.
conftest.pytests/doesn't have__init__.pytests/isn't at the project level, it's inside the pvlib packageHave you tried the vscode insiders build and setting
"python.useIsolation": falsein the User Settings? This supposedly fixes the issue, but either I did it wrong or it didn't work for me. I guess settingpython.useIsolationto false undoes this line:vscode-python/pythonFiles/pyvsc-run-isolated.py
Line 20 in 1079ab2
I guess I'm not terribly concerned b/c pytest works in a terminal and that's good enough for me. And visual debug still works in code, so that's nice. I'm kinda hoping this isn't a larger issue, and it just works itself out in time. 😏
mikofski commentedon Mar 31, 2021
fixed in pvlib/pvlib-python#1204
mikofski commentedon Mar 31, 2021
related to discussion #15494
FrancescoValerio commentedon Apr 28, 2021
I would like to add that this also solved my bug.
This being that pytest or in other words testing does not respect PYTHONPATH when it is in the root directory.
The fix, that is, adding "python.useIsolation": false` to Preferences: Open Settings (JSON) solved the following issue:
add the parent directory that foo and test reside in to your PYTHONPATH (with Mac this is done through sudo nano /private/etc/zprofile and then add :
export PYTHONPATH=~/folder/to/example_parentopen vscode
within vscode open example_parent
press ctrl+shift+p and then type python all tests
enable and configure pytest in the root directory
run pytest within vscode through the python run all tests setting with vscode opened in root
You will receive the following error