Closed mikofski closed 3 years ago
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?
@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.
Thank you very much!!!
Here is the Python test log output:
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:
{
"python.testing.pytestArgs": [],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"python.pythonPath": "venv\\Scripts\\python.exe"
}
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!
The python extension updated to 2021.2.1 (19 February 2021) v2021.2.582707922 but unfortunately, the tests are still not working.
the Python test log output:
python c:\Users\mikm\.vscode\extensions\ms-python.python-2021.2.582707922\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir c:\Users\mikm\Projects\pvlib-python -s --cache-clear
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
the python output:
The settings.json
file:
{
"python.testing.pytestArgs": [],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true,
"python.pythonPath": "venv\\Scripts\\python.exe"
}
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.
Test discovery works!
Run test in debug mode works!
Unfortunately, setting "python.testing.cwd": "pvlib"
in settings.json
doesn't alter the outcome. Discover tests still works, debug mode still works, but run tests fails with the same output as before.
So far the only thing that seems to work is to install using python setup.py install
but using pip install -e .
or python setup.py develop
both fail.
I should also note that if create a conda environment (instead of a python virtual environment) I get the same outcome: discovery works, pytest works from terminal, run tests fails with the same error:
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
@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.
@wholmgren I found a sneaky but unpleasant workaround. I insert this into pvlib/tests/conftest.py
right before import pvlib
:
import sys
sys.path.append(str(Path(__file__).parent.parent.parent))
If I open a python interpreter and print the Python path the outcome of the "editable" install is that pvlib-python
is last in the list:
>>> import pprint
>>> pprint.pprint(sys.path)
['',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/mikm/Projects/pvlib-python/venv/lib/python3.8/site-packages',
'/home/mikm/Projects/pvlib-python']
but printing out the Python path from inside pvlib/tests/conftest.py
:
import pprint
pprint.pprint(sys.path)
shows during test execution it's been removed:
['/home/mikm/Projects/pvlib-python/pvlib/tests',
'/home/mikm/.vscode-server/extensions/ms-python.python-2021.2.582707922/pythonFiles',
'/usr/lib/python38.zip',
'/usr/lib/python3.8',
'/usr/lib/python3.8/lib-dynload',
'/home/mikm/Projects/pvlib-python/venv/lib/python3.8/site-packages']
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. https://github.com/microsoft/vscode-python/issues/14570#issuecomment-755445657
That worked for me.
@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 packageHave 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:
https://github.com/microsoft/vscode-python/blob/1079ab24353c605629b0308b593543fddc78f7f7/pythonFiles/pyvsc-run-isolated.py#L20
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. 😏
related to discussion https://github.com/microsoft/vscode-python/discussions/15494
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:
def return_4():
return 4
from foo.main import return_4
def test_4(): assert return_4() != 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```
5. open vscode
6. within vscode open example_parent
7. press ctrl+shift+p and then type python all tests
8. enable and configure pytest in the root directory
9. run pytest within vscode through the python run all tests setting with vscode opened in root
10. 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
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
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.
We have written the needed data into your cl
Steps to Reproduce:
$py -3.6 -m venv venv
pip install --no-deps -e .
setup.cfg
as only settingconftest.py
(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.
Related Issues