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

test explorer cannot import .env #21632

Closed
QuXing9 opened this issue Jul 14, 2023 · 9 comments
Closed

test explorer cannot import .env #21632

QuXing9 opened this issue Jul 14, 2023 · 9 comments
Assignees
Labels
area-testing info-needed Issue requires more information from poster

Comments

@QuXing9
Copy link

QuXing9 commented Jul 14, 2023

Type: Bug

I created the .env file and defined the PYTHONPATH environment variable in my Python project. When I used test explorer to run test case, I got the failure Failed to import test module:helloworld_test in OUTPUT when Python extension for Visual Studio Code version was 2023.12.0, 2023.10.1, 2023.10.0. it will not fail in other lower versions of the extension.

My project directory is:

  • .vscode
    • launch.json
    • settings.json
  • src
    • helloworld.py
  • tests
    • hellowrold_test.py
  • .env

setting.json is:

{
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.unittestEnabled": true,
    "python.testing.unittestArgs": [
      "-v",
      "-s",
      "./tests",
      "-p",
      "*test.py"
    ],
    "python.testing.pytestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.terminal.activateEnvInCurrentTerminal": true
}

src/helloworld.py is:

class HelloWorld:
    def method1(self):
        print("helloworld")

tests/helloworld_test.py is:

import unittest
from helloworld import HelloWorld

class HelloWorldTest(unittest.TestCase):
    def test_helloworld(self):
        HelloWorld().method1()

.env is

PYTHONPATH=src:

Behaviour

Expected vs. Actual

Actual

report an error:

2023-07-14 14:25:06.824 [error] Unittest test discovery error 
 Failed to import test module: helloworld_test
Traceback (most recent call last):
  File "xxx/lib/python3.9/unittest/loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "xxx/lib/python3.9/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "xxx/pythonProject/tests/helloworld_test.py", line 2, in <module>
    from helloworld import HelloWorld
ModuleNotFoundError: No module named 'helloworld'

Expected:

discovery the testcase successfully in test explorer

Steps to reproduce:

  1. create a project mentioned above
  2. click refresh explorer button
  3. get the error in OUTPUT

Extension version: 2023.12.0
VS Code version: Code 1.80.1 (74f6148eb9ea00507ec113ec51c489d6ffb4b771, 2023-07-12T17:20:23.298Z)
OS version: Darwin x64 22.4.0

System Info
Item Value
CPUs Intel(R) Core(TM) i7-10700K CPU @ 3.80GHz (16 x 3800)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
metal: disabled_off
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
Load (avg) 2, 2, 2
Memory (System) 32.00GB (1.39GB free)
Process Argv --crash-reporter-id 0dd91590-8a1f-4658-ba66-9d560eaf086f
Screen Reader no
VM 0%
Item Value
Remote SSH: devcloud_xxing
OS Linux x64 5.4.119-1-tlinux4-0008
CPUs AMD EPYC 7K62 48-Core Processor (8 x 2595)
Memory (System) 15.38GB (13.56GB free)
VM 0%
A/B Experiments
vsliv368cf:30146710
vsreu685:30147344
python383:30185418
vspor879:30202332
vspor708:30202333
vspor363:30204092
vstes627:30244334
vslsvsres303:30308271
vserr242:30382549
pythontb:30283811
vsjup518:30340749
pythonptprofiler:30281270
vsdfh931cf:30280410
vshan820:30294714
vstes263:30335439
vscorecescf:30445987
vscod805:30301674
binariesv615:30325510
bridge0708:30335490
bridge0723:30353136
vsaa593cf:30376535
pythonvs932:30410667
py29gd2263:30789496
vsclangdf:30486550
c4g48928:30535728
dsvsc012:30540252
pynewext54:30695312
azure-dev_surveyone:30548225
3biah626:30602489
pyind779:30671433
89544117:30613380
pythonsymbol12:30671437
showlangstatbar:30737416
vsctsb:30748421
pythonms35:30701012
a2ce3375:30757347
24365598:30736109
pythonfmttext:30731395
pythoncmvfstrcf:30756944
fixshowwlkth:30771522
hideindicator:30785051
pythongtdpath:30769146
i26e3531:30780429
pythonnosmt12:30779714
pythonidxpt:30784022
pythonnocebcf:30776496
h7j2d465:30786200
dsvsc013:30789517

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Jul 14, 2023
@eleanorjboyd eleanorjboyd self-assigned this Jul 14, 2023
@eleanorjboyd
Copy link
Member

Hello! Will investigate now- in the meantime could you send your full logs from the python output? I want to see if the experiment related to testing is turned on. Thanks

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Jul 14, 2023
@QuXing9
Copy link
Author

QuXing9 commented Jul 16, 2023

The OUTPUT info is:

2023-07-16 14:42:09.590 [info] Experiment 'pythonPromptNewFormatterExt' is active
2023-07-16 14:42:09.590 [info] Experiment 'pythonPromptNewToolsExt' is active
2023-07-16 14:42:09.590 [info] Experiment 'pythonSurveyNotificationcf' is active
2023-07-16 14:42:09.590 [info] Experiment 'pythonTestAdapter' is active
2023-07-16 14:42:09.590 [info] Test server listening.
2023-07-16 14:42:09.590 [info] Python interpreter path: /usr/bin/python3
2023-07-16 14:42:09.590 [warning] Retry failed, storage update failed for key PYTHON_GLOBAL_STORAGE_KEYS
2023-07-16 14:42:09.590 [warning] Retry failed, storage update failed for key PYTHON_GLOBAL_STORAGE_KEYS
2023-07-16 14:42:09.630 [warning] Retry failed, storage update failed for key PYTHON_GLOBAL_STORAGE_KEYS
2023-07-16 14:42:09.630 [warning] Retry failed, storage update failed for key PYTHON_GLOBAL_STORAGE_KEYS
2023-07-16 14:42:09.630 [warning] Retry failed, storage update failed for key PYTHON_GLOBAL_STORAGE_KEYS
2023-07-16 14:42:09.630 [warning] Retry failed, storage update failed for key PYTHON_GLOBAL_STORAGE_KEYS
2023-07-16 14:42:09.630 [warning] Retry failed, storage update failed for key PYTHON_GLOBAL_STORAGE_KEYS
2023-07-16 14:42:09.685 [info] > conda info --json
2023-07-16 14:42:09.690 [info] > /usr/bin/python3 -I ~/.vscode/extensions/ms-python.python-2023.12.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2023.12.0/pythonFiles/interpreterInfo.py
2023-07-16 14:42:10.578 [info] Starting Pylance language server.
2023-07-16 14:42:21.351 [info] Discover tests for workspace name: pythonProject - uri: /Users/xxing/Documents/project/pythonProject
2023-07-16 14:42:21.351 [info] Running discovery for unittest using the new test adapter.
2023-07-16 14:42:21.380 [info] Running unittests with arguments: /Users/xxing/.vscode/extensions/ms-python.python-2023.12.0/pythonFiles/unittestadapter/discovery.py,--port,58101,--uuid,1c1ec181-2296-4760-8483-09ddb1ae2633,--udiscovery,-v,-s,./tests,-p,*test.py

2023-07-16 14:42:21.380 [info] > /usr/bin/python3 ~/.vscode/extensions/ms-python.python-2023.12.0/pythonFiles/unittestadapter/discovery.py --port 58101 --uuid 1c1ec181-2296-4760-8483-09ddb1ae2633 --udiscovery -v -s ./tests -p *test.py
2023-07-16 14:42:21.380 [info] cwd: .
2023-07-16 14:42:21.472 [info] Test server connected to a client.
2023-07-16 14:42:21.473 [info] Using result resolver for discovery
2023-07-16 14:42:21.474 [error] Unittest test discovery error
Failed to import test module: helloworld_test
Traceback (most recent call last):
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 436, in _find_test_path
module = self._get_module_from_name(name)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 377, in _get_module_from_name
import(name)
File "/Users/xxing/Documents/project/pythonProject/tests/helloworld_test.py", line 2, in
from helloworld import HelloWorld
ModuleNotFoundError: No module named 'helloworld'

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Jul 16, 2023
@eleanorjboyd
Copy link
Member

eleanorjboyd commented Jul 17, 2023

Thank you! Yes it looks like you are on the new testing rewrite and this is why you saw a change in behavior since we just moved to this experiment to a larger group of people receiving it. You can opt out of the rewrite as I get this fix in by setting this in your user settings: "python.experiments.optOutFrom": ["pythonTestAdapter"],. If this doesn't work let me know as this will mean it is a different issue. Thank you and I will send updates in this thread as I get a fix in.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Jul 17, 2023
@eleanorjboyd
Copy link
Member

Hi! I gave this a try and downgraded to version v2023.9.11571010 and am still seeing the discovery error. Which lower versions of the extension were you using?

@eleanorjboyd
Copy link
Member

since #21640 has more comments and seems to be the same issue closing this as a dup of that one. But thank you for the minimal repro as I used that. Please jump to the other thread and add your comments, I just put my most recent comment from this thread in there regarding being unable to repro.

@eleanorjboyd eleanorjboyd closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2023
@eleanorjboyd eleanorjboyd reopened this Jul 17, 2023
@eleanorjboyd
Copy link
Member

reopening as it is a different error message from the other one

@QuXing9
Copy link
Author

QuXing9 commented Jul 19, 2023

Thank you! Yes it looks like you are on the new testing rewrite and this is why you saw a change in behavior since we just moved to this experiment to a larger group of people receiving it. You can opt out of the rewrite as I get this fix in by setting this in your user settings: "python.experiments.optOutFrom": ["pythonTestAdapter"],. If this doesn't work let me know as this will mean it is a different issue. Thank you and I will send updates in this thread as I get a fix in.

This solved my problem, thx!

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Jul 19, 2023
@QuXing9
Copy link
Author

QuXing9 commented Jul 19, 2023

Hi! I gave this a try and downgraded to version v2023.9.11571010 and am still seeing the discovery error. Which lower versions of the extension were you using?

works on v2023.8.0

@eleanorjboyd
Copy link
Member

Hi! Following up, from my understanding unittest does not actually inherit from the .env file automatically and something like this code below is used to get the envvars. If you have a reference saying that unittest should inherit from .env please re-open this issue. Thanks!

from dotenv import load_dotenv
import os
load_dotenv('.env')

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

No branches or pull requests

2 participants