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

ImportError while loading conftest #23218

Closed
Akopov4 opened this issue Apr 10, 2024 · 12 comments
Closed

ImportError while loading conftest #23218

Akopov4 opened this issue Apr 10, 2024 · 12 comments
Assignees
Labels
area-testing triage-needed Needs assignment to the proper sub-team

Comments

@Akopov4
Copy link

Akopov4 commented Apr 10, 2024

Type: Bug

In my project I have a folder helpers. There is a python file db.py. There is a class called Database.
In the same level as folders helpers i have file conftest.py . In conftest I import Database class.
"from .helpers.db import DataBase" . There are no errors shown in the conftest.py file.
But when I run tests discovery, I get an error:

Test loading failed: Error: Process exited with code 4: ImportError while loading conftest '/home/akop/PythonProjects/playwright_qa_mamania/conftest.py'.
conftest.py:7: in <module>
    from .helpers.db import DataBase
E   ImportError: attempted relative import with no known parent package

Error: Process exited with code 4: ImportError while loading conftest '/home/akop/PythonProjects/playwright_qa_mamania/conftest.py'.
conftest.py:7: in <module>
    from .helpers.db import DataBase
E   ImportError: attempted relative import with no known parent package

    at ChildProcess.<anonymous> (/home/akop/.vscode/extensions/littlefoxteam.vscode-python-test-adapter-0.8.2/out/src/processRunner.js:27:32)
    at Object.onceWrapper (node:events:632:26)
    at ChildProcess.emit (node:events:517:28)
    at ChildProcess.emit (node:domain:489:12)
    at maybeClose (node:internal/child_process:1098:16)
    at Socket.<anonymous> (node:internal/child_process:450:11)
    at Socket.emit (node:events:517:28)
    at Socket.emit (node:domain:489:12)
    at Pipe.<anonymous> (node:net:350:12)

Extension version: 2024.4.0
VS Code version: Code 1.88.0 (5c3e652f63e798a5ac2f31ffd0d863669328dc4c, 2024-04-03T13:25:57.039Z)
OS version: Linux x64 5.15.0-102-generic
Modes:

System Info
Item Value
CPUs Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz (4 x 2500)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off
Load (avg) 4, 4, 3
Memory (System) 11.56GB (2.59GB free)
Process Argv --unity-launch --crash-reporter-id 01400cc3-0ed1-484c-a547-9fad9bdfb52d
Screen Reader no
VM 0%
DESKTOP_SESSION cinnamon
XDG_CURRENT_DESKTOP X-Cinnamon
XDG_SESSION_DESKTOP cinnamon
XDG_SESSION_TYPE x11
A/B Experiments
vsliv368cf:30146710
vspor879:30202332
vspor708:30202333
vspor363:30204092
vswsl492:30256859
vscod805:30301674
binariesv615:30325510
vsaa593:30376534
py29gd2263:30899288
c4g48928:30535728
azure-dev_surveyone:30548225
962ge761:30959799
pythongtdpath:30769146
welcomedialog:30910333
pythonidxpt:30866567
pythonnoceb:30805159
asynctok:30898717
pythontestfixt:30902429
pythonregdiag2:30936856
pyreplss1:30897532
pythonmypyd1:30879173
pythoncet0:30885854
h48ei257:31000450
pythontbext0:30879054
accentitlementsc:30995553
dsvsc016:30899300
dsvsc017:30899301
dsvsc018:30899302
d6fi2104:30959263
cppperfnew:31000557
d34g3935:30971562
fegfb526:30981948
bg6jg535:30979843
ccp1r3:30993539
dsvsc020:30976470
pythonait:31006305
gee8j676:31009558
dsvsc021:30996838
gd77d436:30999572

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Apr 10, 2024
@karthiknadig
Copy link
Member

@Akopov4 Relative imports work differently based on where you are running from and what you are using to run. Wehn you open conftest.py, it is as if you are running that file, from the context of that file, the imports can get resolved. But, typically conftest gets loaded into pytest and this can mean that it is looking for a __init__.py that it can use as a reference package to find the .helpers.db.

Please provide how you have organized your code, so we can help with this.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Apr 10, 2024
@Akopov4
Copy link
Author

Akopov4 commented Apr 11, 2024

Peek 2024-04-11 08-03

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Apr 11, 2024
@Akopov4
Copy link
Author

Akopov4 commented Apr 11, 2024

@karthiknadig please look my gif

@Akopov4
Copy link
Author

Akopov4 commented Apr 11, 2024

@eleanorjboyd please look my gif

@karthiknadig
Copy link
Member

Does it work if you run pytest from terminal? Just run pytest tests?

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Apr 11, 2024
@Akopov4
Copy link
Author

Akopov4 commented Apr 11, 2024

i don't run pytest. I just hit the button to discover tests

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Apr 11, 2024
@karthiknadig
Copy link
Member

I understand that you use the run button. But I want to know if it works from the terminal. When you run it from terminal does it execute as expected or do you get error?

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Apr 11, 2024
@Akopov4
Copy link
Author

Akopov4 commented Apr 11, 2024

I don't use run button. I use button - Test Explorer: Reload Tests

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Apr 11, 2024
@Akopov4
Copy link
Author

Akopov4 commented Apr 11, 2024

There problem was in "dot":
from .helpers.db import DataBase
Without "dot" everything works ok:
from helpers.db import DataBase

  • but it is strange

@ssbarnea
Copy link

ssbarnea commented May 6, 2024

We should reopen this bug because is genuine. I was able to encounter it myself while my python environment is fine and I suspect that I found some of the causes.

This is output from Extension Host and it surprisingly includes ANSI escapes, reason for likely being the fact that I have PY_COLORS=1 added to my user environment. I think that pytest adapter must explicitely disable ANSI output if it does not support it (or strip them), so environment variables would not confuse it.

2024-04-29 14:59:51.590 [error] Error: �[31mImportError while loading conftest '/Users/ssbarnea/c/a/ansible-lint/test/conftest.py'.�[0m
�[31m�[1m�[31mtest/conftest.py�[0m:13: in <module>�[0m
�[31m    �[0m�[94mfrom�[39;49;00m �[04m�[96mansiblelint�[39;49;00m�[04m�[96m.�[39;49;00m�[04m�[96mtesting�[39;49;00m�[04m�[96m.�[39;49;00m�[04m�[96mfixtures�[39;49;00m �[94mimport�[39;49;00m *  �[90m# noqa: F403�[39;49;00m�[90m�[39;49;00m�[0m
�[31m�[1m�[31msrc/ansiblelint/testing/__init__.py�[0m:13: in <module>�[0m
�[31m    �[0m�[94mfrom�[39;49;00m �[04m�[96mansiblelint�[39;49;00m�[04m�[96m.�[39;49;00m�[04m�[96mapp�[39;49;00m �[94mimport�[39;49;00m get_app�[90m�[39;49;00m�[0m
�[31m�[1m�[31msrc/ansiblelint/app.py�[0m:17: in <module>�[0m
�[31m    �[0m�[94mfrom�[39;49;00m �[04m�[96mansiblelint�[39;49;00m �[94mimport�[39;49;00m formatters�[90m�[39;49;00m�[0m
�[31m�[1m�[31msrc/ansiblelint/formatters/__init__.py�[0m:13: in <module>�[0m
�[31m    �[0m�[94mfrom�[39;49;00m �[04m�[96mansiblelint�[39;49;00m�[04m�[96m.�[39;49;00m�[04m�[96mconfig�[39;49;00m �[94mimport�[39;49;00m options�[90m�[39;49;00m�[0m
�[31m�[1m�[31mE     File "/Users/ssbarnea/c/a/ansible-lint/src/ansiblelint/config.py", line 181�[0m�[0m
�[31m�[1m�[31mE       return return bool(int(os.environ.get("ANSIBLE_LINT_NODEPS", "0")))�[0m�[0m
�[31m�[1m�[31mE              ^^^^^^�[0m�[0m
�[31m�[1m�[31mE   SyntaxError: invalid syntax�[0m�[0m

Traceback (most recent call last):
  File "/Users/ssbarnea/.vscode/extensions/ms-python.python-2024.4.1/python_files/testing_tools/run_adapter.py", line 22, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/Users/ssbarnea/.vscode/extensions/ms-python.python-2024.4.1/python_files/testing_tools/adapter/__main__.py", line 97, in main
    parents, result = run(toolargs, **subargs)
                      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/ssbarnea/.vscode/extensions/ms-python.python-2024.4.1/python_files/testing_tools/adapter/pytest/_discovery.py", line 47, in discover
    raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 4)

Note: the file does not really have a syntax error, python runs it quite fine. Still, the way vscode is trying to load it makes it throw this weird error. I suspect that the import at the beginning of the file does not work and that is later causing a syntax error when trying to call.

@eleanorjboyd
Copy link
Member

Hi! The issue with the syntax error and coloration will be fixed as part of this issue: #21845 it is a known problem that it works in the test result panel (during run) but not in the python output panel (output for error / discovery)

Secondly I am seeing you are using the old testing architecture: File "/Users/ssbarnea/.vscode/extensions/ms-python.python-2024.4.1/python_files/testing_tools/adapter/pytest/_discovery.py", line 47, in discover. Could you update to our rewrite and try again? You can do so adding this setting to your users settings.json "python.experiments.optInto": ["pythonTestAdapter"]. Then you can confirm its enabled by finding Experiment 'pythonTestAdapter' is active in your python logs. If you are unable to see the log for the experiment change your log level to trace and it should show up.
To set to trace use theDeveloper: set log level command in the command palette. Thanks

@github-actions github-actions bot added the info-needed Issue requires more information from poster label May 6, 2024
@Akopov4
Copy link
Author

Akopov4 commented May 7, 2024

@eleanorjboyd @ssbarnea I have created new defect, because this one was closed. I attached there gif file. I hope it will make issue clear #23328

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label May 7, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

4 participants