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

virtual env is ignored while testing #23408

Open
cupcat121 opened this issue May 10, 2024 · 9 comments
Open

virtual env is ignored while testing #23408

cupcat121 opened this issue May 10, 2024 · 9 comments
Assignees
Labels
area-testing bug Issue identified by VS Code Team member as probable bug

Comments

@cupcat121
Copy link

Type: Bug

Behaviour

Even if I installed pytest in the venv and selected the interpretor, it still use the pytest of my system while testing. I had to modify python.testing.pytestPath to ${workspaceFolder}/.venv/bin/pytest to get it worked.

There is no error performing my tests in the terminal within the venv activated

Steps to reproduce:

  1. create a project and an virtual env without pip
  2. install pytest in the virtual env
  3. install the project as editable mode with pip install -e
  4. add a test file which contains import of the project from myproject import foo
  5. run test in the sidebar

Diagnostic data

python test log panel

=============================
platform linux -- Python 3.12.3, pytest-8.1.2, pluggy-1.4.0
rootdir: /mnt/lib/develop/sources/python/cphub
configfile: pyproject.toml
plugins: typeguard-4.2.1, anyio-4.3.0, mock-3.14.0
collected 0 items / 1 error

==================================== ERRORS ====================================
___________________ ERROR collecting tests/test_pastebin.py ____________________
ImportError while importing test module '/mnt/lib/develop/sources/python/cphub/tests/test_pastebin.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_pastebin.py:4: in <module>
    from cphub.pastebin import PasteBin
E   ModuleNotFoundError: No module named 'cphub'
-------------- generated xml file: /tmp/tmp-88939gx50f6ouwSFD.xml --------------
=========================== short test summary info ============================
ERROR tests/test_pastebin.py
=============================== 1 error in 0.03s ===============================
ERROR: found no collectors for /mnt/lib/develop/sources/python/cphub/tests/test_pastebin.py::test_singleton_instance
Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

2024-05-10 17:59:35.532 [info] > ~/.local/bin/pytest --rootdir . --override-ini junit_family=xunit1 --junit-xml=/tmp/tmp-88939gx50f6ouwSFD.xml ./tests/test_pastebin.py::test_singleton_instance
2024-05-10 17:59:35.533 [info] cwd: .

Extension version: 2024.7.11301009
VS Code version: Code - Insiders 1.90.0-insider (ccba2fc85c14bedeef5a72b7ebc0bde9b0e91948, 2024-05-06T08:06:22.239Z)
OS version: Linux x64 6.8.9-zen1-1-zen
Modes:

  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.14
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
  • Value of the python.languageServer setting: Pylance
User Settings


venvPath: "<placeholder>"

venvFolders: "${workspaceFolder}/.venv"

languageServer: "Pylance"

testing
• cwd: "${workspaceFolder}"
• pytestArgs: "${workspaceFolder}/tests"
• pytestEnabled: true
• pytestPath: "<placeholder>"

Installed Extensions
Extension Name Extension Id Version
Auto Close Tag formulahendry.auto-close-tag 0.5.15
Auto Rename Tag formulahendry.auto-rename-tag 0.1.10
Even Better TOML tamasfe.even-better-toml 0.19.2
gitignore codezombiech.gitignore 0.9.0
Indenticator SirTori.indenticator 0.7.0
IntelliCode VisualStudioExptTeam.vscodeintellicode 1.3.1
IntelliCode API Usage Examples VisualStudioExptTeam.intellicode-api-usage-examples 0.2.8
json ZainChen.json 2.0.2
Live Server ritwickdey.LiveServer 5.7.9
Markdown All in One yzhang.markdown-all-in-one 3.6.2
Path Intellisense christian-kohler.path-intellisense 2.8.5
Prettify JSON mohsen1.prettify-json 0.0.3
Project Manager alefragnani.project-manager 12.8.0
Pylance ms-python.vscode-pylance 2024.5.1
Python ms-python.python 2024.7.11301009
Python Debugger ms-python.debugpy 2024.6.0
Python Extension Pack donjayamanne.python-extension-pack 1.7.0
Python Indent KevinRose.vsc-python-indent 1.18.0
Python Test Explorer for Visual Studio Code littlefoxteam.vscode-python-test-adapter 0.8.2
Python Type Hint njqdev.vscode-python-typehint 1.5.1
Remote - SSH ms-vscode-remote.remote-ssh 0.112.2024050815
Remote - SSH: Editing Configuration Files ms-vscode-remote.remote-ssh-edit 0.86.0
Remote Explorer ms-vscode.remote-explorer 0.5.2024031109
Remote Repositories ms-vscode.remote-repositories 0.41.2024022202
Ruff charliermarsh.ruff 2024.20.0
Table Visualizer for JavaScript Profiles ms-vscode.vscode-js-profile-table 1.0.9
Tabnine AI TabNine.tabnine-vscode 3.98.0
Test Adapter Converter ms-vscode.test-adapter-converter 0.1.9
Test Explorer UI hbenl.vscode-test-explorer 2.21.1
Todo Tree Gruntfuggly.todo-tree 0.0.226
Tokyo Night enkia.tokyo-night 1.0.6
Wal Theme dlasagno.wal-theme 1.2.0
YAML redhat.vscode-yaml 1.14.0
System Info
Item Value
CPUs AMD Ryzen 5 3600 6-Core Processor (12 x 4016)
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) 1, 1, 1
Memory (System) 15.51GB (8.74GB free)
Process Argv --unity-launch --enable-features=UseOzonePlatform --ozone-platform=wayland --password-store=gnome --enable-wayland-ime
Screen Reader no
VM 0%
DESKTOP_SESSION plasma
XDG_CURRENT_DESKTOP KDE
XDG_SESSION_DESKTOP plasma
XDG_SESSION_TYPE wayland
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label May 10, 2024
@tzaffi
Copy link

tzaffi commented May 10, 2024

Identical problem since I updated VS Code yesterday. My PyTest is borked on VSCode (but works fine on the terminal).

@karthiknadig
Copy link
Member

karthiknadig commented May 13, 2024

@cupcat121 it looks like you have set a path to pytest via settings to point to ~/.local/bin/pytest. That will use whatever environment pytest is installed at. We don't control that scenario. The pytest binary will pick the python based on the environment it is in.

If you have a .venv environment and you want to use a global ~/.local/bin/pytest. That is not a supported scenario. You can try launching VS Code from an activated environment from a external terminal and see if it helps.

Is there any reason you want a venv without pip?

@github-actions github-actions bot added the info-needed Issue requires more information from poster label May 13, 2024
@karthiknadig
Copy link
Member

karthiknadig commented May 13, 2024

@cupcat121 Also doing pip install -e will install in the global environment, as you mentioned your virtual environment does not have pip that means that this is getting installed to be loaded with global environment. See here for more info https://snarky.ca/why-you-should-use-python-m-pip/

Note: If you create virtual environment using VS Code Create environment command, it can try to install pip into the virtual environment for you.

@tzaffi
Copy link

tzaffi commented May 13, 2024

@karthiknadig - are you suggesting that if I use VS Code Create vs. my own bespke virtual env, pytest should work fine?

@karthiknadig
Copy link
Member

karthiknadig commented May 13, 2024

@tzaffi Not really. If you have a User settings.json (global settings) with python.testing.pytestPath set to ~/.local/bin/pytest. Then, all workspaces will use that pytest and the environment that pytest was installed in. This is because of how ~/.local/bin/pytest actually is designed:
image

It will start /usr/bin/python3 (from line 1), and use that as the environment. That means if you have a workspace specific environment, then you will have to point python.testing.pytestPath to the workspace specific one. Otherwise, it will use the wrong environment. So, to make it work you will have to set python.testing.pytestPath to ${workspaceFolder}/.venv/bin/pytest. The .venv/bin/pytest just points to .venv/bin/python and starts pytest by importing it from that environment.
image

My suggestion is remove the global python.testing.pytestPath setting from User settings, and also remove it from workspace settings. Python extension will pick up the one in your environment automatically, based on the selected python. Python extension just runs <selected python> -m pytest so you will get the right environment.

@andrewstevens-infineon
Copy link

Note that although Run Test action does not execute pytest in the select venv's environment the Debug Test action does . See #23467.

Copy link

Hey @eleanorjboyd, this issue might need further attention.

@cupcat121, you can help us out by closing this issue if the problem no longer exists, or adding more information.

Copy link

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.

Happy Coding!

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Aug 29, 2024
@eleanorjboyd eleanorjboyd removed the info-needed Issue requires more information from poster label Sep 3, 2024
@eleanorjboyd eleanorjboyd reopened this Sep 3, 2024
@andrewstevens-infineon
Copy link

Thanks Eleanor. The underlying problem #23467 (Run Test not "honouring" the specified venv as"Debug Test" does) is pretty clear.

@eleanorjboyd eleanorjboyd removed the triage-needed Needs assignment to the proper sub-team label Dec 16, 2024
@karthiknadig karthiknadig added the bug Issue identified by VS Code Team member as probable bug label Dec 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-testing bug Issue identified by VS Code Team member as probable bug
Projects
None yet
Development

No branches or pull requests

5 participants