microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.34k stars 1.19k forks source link

The conda environment doesn't work with test explorer #19873

Closed xarpy closed 1 year ago

xarpy commented 2 years ago

Type: Bug

Behaviour

I participated in some projects that use python version 2.7 and the Pyramid framework. As all my environments are managed through miniconda3, I just create the specific environments for each project from their requirements.

However, I have noticed some instability in the vscode to be able to debug some projects, which use python, since 2 last updates.

Now during some routine tests, as I always had the habit of running the debugger and creating breakpoints, as well as running the tests from the editor, I'm not succeeding anymore.

This my launch.json for the project reference:

{
    "configurations": [
        {
            "name": "Python: Pyramid Application",
            "type": "python",
            "request": "launch",
            "module": "pyramid.scripts.pserve",
            "args": ["--reload", "${workspaceFolder}/development.ini"],
            "pyramid": true,
            "jinja": true,
            "justMyCode": true
        }
    ]
}

Trying to run the debugger I can't get any kind of output, but running the Plugin Test Explorer UI + Python Test Explorer, which always worked before, now doesn't work!

Please note that I can activate the python environments created by conda normally from vscode, as well as run all the tests in a terminal:

conda run -n project_name --no-capture-output --live-stream python ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/testing_tools/run_adapter.py  pytest -- --rootdir . -s --cache-clear
>>>PYTHON-EXEC-OUTPUT
<<<PYTHON-EXEC-OUTPUT
Traceback (most recent call last):
  File "/home/xarpy/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/get_output_via_markers.py", line 26, in <module>
    runpy.run_path(module, run_name="__main__")
  File "/home/xarpy/opt/miniconda3/lib/python3.9/runpy.py", line 288, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/home/xarpy/opt/miniconda3/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/xarpy/opt/miniconda3/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/xarpy/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/testing_tools/run_adapter.py", line 17, in <module>
    from testing_tools.adapter.__main__ import parse_args, main
  File "/home/xarpy/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/testing_tools/adapter/__main__.py", line 9, in <module>
    from . import pytest, report
  File "/home/xarpy/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/testing_tools/adapter/pytest/__init__.py", line 7, in <module>
    from ._discovery import discover
  File "/home/xarpy/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/testing_tools/adapter/pytest/_discovery.py", line 8, in <module>
    import pytest
ModuleNotFoundError: No module named 'pytest'
ERROR conda.cli.main_run:execute(49): `conda run python ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/testing_tools/run_adapter.py pytest -- --rootdir . -s --cache-clear` failed. (See above for error)

When running debugpy command with conda run in terminal, for test if conda find the module and package to execute:

conda run -n project_name --no-capture-output --live-stream python ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/lib/python/debugpy/adapter
Content-Length: 137

{"seq": 1, "type": "event", "event": "output", "body": {"category": "telemetry", "output": "ptvsd", "data": {"packageVersion": "1.6.3"}}}Content-Length: 139

{"seq": 2, "type": "event", "event": "output", "body": {"category": "telemetry", "output": "debugpy", "data": {"packageVersion": "1.6.3"}}}

Expected vs. Actual

XXX

Steps to reproduce:

  1. Just create the files, for debugger preffer to pyramid framework, install the plugin Test Explorer UI and try to execute debug or test explorer.

Diagnostic data

User Settings

``` condaPath: "" languageServer: "Pylance" linting • flake8Args: "" • flake8Enabled: true formatting • autopep8Args: "" testing • pytestEnabled: true • autoTestDiscoverOnSaveEnabled: false ```

Extension version: 2022.14.0 VS Code version: Code 1.71.2 (74b1f979648cc44d385a2286793c226e611f59e7, 2022-09-14T21:12:14.256Z) OS version: Linux x64 5.15.0-48-generic Modes: Sandboxed: No

System Info |Item|Value| |---|---| |CPUs|11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz (16 x 2300)| |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_renderer: enabled_on
video_decode: disabled_software
video_encode: disabled_software
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off| |Load (avg)|1, 0, 0| |Memory (System)|15.48GB (8.83GB free)| |Process Argv|--unity-launch --crash-reporter-id af58d562-3f25-41a2-a7ce-ababeb5e1431| |Screen Reader|no| |VM|0%| |DESKTOP_SESSION|ubuntu| |XDG_CURRENT_DESKTOP|Unity| |XDG_SESSION_DESKTOP|ubuntu| |XDG_SESSION_TYPE|x11|
A/B Experiments ``` vsliv368cf:30146710 vsreu685:30147344 python383cf:30185419 vspor879:30202332 vspor708:30202333 vspor363:30204092 vswsl492:30256859 vstes627:30244334 vslsvsres303:30308271 pythonvspyl392:30443607 vserr242cf:30382550 pythontb:30283811 vsjup518:30340749 pythonptprofiler:30281270 vshan820:30294714 vstes263:30335439 pythondataviewer:30285071 vscod805:30301674 binariesv615:30325510 bridge0708:30335490 bridge0723:30353136 cmake_vspar411:30557514 vsaa593:30376534 pythonvs932:30410667 cppdebug:30492333 vsclangdc:30486549 c4g48928:30535728 hb751961:30553087 dsvsc012cf:30540253 azure-dev_surveyone:30548225 2144e591:30553903 fc301958:30573243 ```
ezra-en commented 2 years ago

Seeing as you can run it manually, it might be an issue with the debugger not being able to attach to your miniconda interpreter, or because your code isn't being recognised as your own because you're running it with miniconda. Try debugging with 'justMyCode': false and see if that works. If so, this may be a duplicate of #19871.

xarpy commented 2 years ago

Hi @EzraEn1 , one of the approaches suggested in some comments was to downgrade to previous versions of the ms-python package, to version v2022.2.1924087327, This solved my problem partially, but I will test with these recommendations and try to understand the scenarios better.

xarpy commented 2 years ago

Hi @EzraEn1 and @karrtikr , the solution with this property does not work, because vsocde does not work in debug mode with the last update and conda, but if you change to the previous version of the ms-python package, as I suggested in the last post, it works, however now tests do not work anymore!

I am currently using the latest version of conda, 22.9.0, but still no success.

Hey @DonJayamanne , any suggestions?

karrtikr commented 2 years ago

This might be that when run in VS Code it is not running in a activated environment. Can you try this?

  1. Anaconda prompt and cd to your workspace.
  2. Activate the environment using conda activate
  3. Start VS Code using code .

related #10626

If that doesn't work, please change the setting terminal.integrated.inheritEnv to false as outlined in https://code.visualstudio.com/updates/v1_36#_launch-terminals-with-clean-environments.

karrtikr commented 2 years ago

Also, can you run

conda run -n project_name --no-capture-output --live-stream python ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/testing_tools/run_adapter.py  pytest -- --rootdir . -s --cache-clear

in VSCode terminal and send me the output. Make sure to expand ~.

xarpy commented 2 years ago

This might be that when run in VS Code it is not running in a activated environment. Can you try this?

  1. Anaconda prompt and cd to your workspace.
  2. Activate the environment using conda activate
  3. Start VS Code using code .

related #10626

If that doesn't work, please change the setting terminal.integrated.inheritEnv to false as outlined in https://code.visualstudio.com/updates/v1_36#_launch-terminals-with-clean-environments.

Hi @karrtikr, I followed these steps but was not successful, see the recording below, when I tried running vscode with conda enabled manually and accessing the project folder with vscode from the terminal:: Gravação de tela de 11-10-2022 21_42_29

xarpy commented 2 years ago

Also, can you run

conda run -n project_name --no-capture-output --live-stream python ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/testing_tools/run_adapter.py  pytest -- --rootdir . -s --cache-clear

in VSCode terminal and send me the output. Make sure to expand ~.


conda run -n project_name --no-capture-output --live-stream python ~/.vscode/extensions/ms-python.python-2022.16.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.16.0/pythonFiles/testing_tools/run_adapter.py  pytest -- --rootdir . -s --cache-clear
>>>PYTHON-EXEC-OUTPUT
usage: /home/xarpy/.vscode/extensions/ms-python.python-2022.16.0/pythonFiles/testing_tools/run_adapter.py
       [-h] {discover} ...
/home/xarpy/.vscode/extensions/ms-python.python-2022.16.0/pythonFiles/testing_tools/run_adapter.py: error: argument cmd: invalid choice: 'pytest' (choose from 'discover')
<<<PYTHON-EXEC-OUTPUT
ERROR conda.cli.main_run:execute(49): `conda run python /home/xarpy/.vscode/extensions/ms-python.python-2022.16.0/pythonFiles/get_output_via_markers.py /home/xarpy/.vscode/extensions/ms-python.python-2022.16.0/pythonFiles/testing_tools/run_adapter.py pytest -- --rootdir . -s --cache-clear` failed. (See above for error)

But if I run it in the old version, as talked about earlier, the debugger works but the tests don't, only if they are run manually from the terminal.

karrtikr commented 2 years ago

Debugger doesn't work as we no longer support debugging for Python below 3.7, please upvote https://github.com/microsoft/vscode-python/issues/19559 if you would like to see us bring it back.

karrtikr commented 2 years ago

For the testing issue it seems we're not able to parse the arguments correctly when run in terminal as it looks like here: https://github.com/microsoft/vscode-python/issues/19873#issuecomment-1275464096

Reassigning as that's testing related.

karrtikr commented 2 years ago

@xarpy Can you run the following command instead of https://github.com/microsoft/vscode-python/issues/19873#issuecomment-1271942780?

conda run -n project_name --no-capture-output --live-stream python ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.14.0/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear
karrtikr commented 2 years ago

@xarpy Also try to open a new terminal and install pytest again:

python -m pip install pytest

Ensure conda environment is activated before the above command is run, should look something similar to: image

karrtikr commented 1 year ago

Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on then we will be happy to re-open this issue to pick up where we left off.