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

Pytest (and often other things) loaded with wrong PYTHONPATH #23920

Open
kam193 opened this issue Aug 7, 2024 · 8 comments
Open

Pytest (and often other things) loaded with wrong PYTHONPATH #23920

kam193 opened this issue Aug 7, 2024 · 8 comments
Assignees
Labels
area-testing triage-needed Needs assignment to the proper sub-team

Comments

@kam193
Copy link

kam193 commented Aug 7, 2024

Type: Bug

Behaviour

Since a few weeks / months, I'm constantly falling into issues that VSCode is setting global python path instead of or before the current virtual env when invoking different extension features (tests from test explorer, Jupyter notebooks, even terminal and other Python-dependant extensions). This path was not configured anywhere, even the default interpreter has been changed. The problem existed on two independent machines, without any config sync between them. As a common thing I'd note that both VSCode instances were configured long before VSCode-Python switched to use a storage instead of config file for the Python Interpreter path.

I have managed to resolve most of the issues by removing the ~/.config/Code/Cache directory (maybe also some others), but now it came again, when calling pytest from the Python test explorer.

Basically, pytest works well when called from terminal, but fails when trying to discover any test. Please see the logs from "Output" for Python - the PYTHONPATH is set to contain the global path, and the invoked pytest does try to use pluggy from there instad of the one from venv.

My suspicion is: the global path was at some point saved somewhere by the VSCode-Python extension. As the config files are no longer used for that, how could I looked up what are settings saved by the extension and remove the bad one?

This problem is extreamly irritating, couter-productive and comes back like a bumerang :(

Steps to reproduce:

Extreamly hard to say, I suspect you should have VSCode & the extension installed before the transition, and then do anything that would expect to use the local venv instead of global interpreter.

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

2024-08-07 12:48:33.346 [info] Discover tests for workspace name: *** - uri: /home/***
2024-08-07 12:48:33.346 [info] Running discovery for pytest using the new test adapter.
2024-08-07 12:48:33.346 [info] All environment variables set for pytest discovery: {*** "PYTHONPATH":"/home/kamil/.vscode/extensions/ms-python.python-2024.12.1-linux-x64/python_files:/usr/lib/python3/dist-packages", *** , "PWD":"/home/kamil",*** ,"SHELL":"/bin/bash", *** "VSCODE_AMD_ENTRYPOINT":"vs/workbench/api/node/extensionHostProcess","VSCODE_CLI":"1","VSCODE_CODE_CACHE_PATH":"/home/kamil/.config/Code/CachedData/b1c0a14de1414fcdaa400695b4db1c0799bc3124","VSCODE_CRASH_REPORTER_PROCESS_TYPE":"extensionHost","VSCODE_CWD":"/home/kamil","VSCODE_HANDLES_UNCAUGHT_ERRORS":"true","VSCODE_IPC_HOOK":"/run/user/1000/vscode-a892cd8d-1.92-main.sock","VSCODE_NLS_CONFIG":"{\"userLocale\":\"pl\",\"osLocale\":\"pl-pl\",\"resolvedLanguage\":\"en\",\"defaultMessagesFile\":\"/usr/share/code/resources/app/out/nls.messages.json\",\"locale\":\"en\",\"availableLanguages\":{}}","VSCODE_PID":"31610", *** "ELECTRON_RUN_AS_NODE":"1","APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL":"1","PATH":"/home/kamil/.poetry/bin:/home/kamil/bin:/home/kamil/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin","TEST_RUN_PIPE":"/run/user/1000/python-test-discovery-23875f4f44d677193539.sock"}
2024-08-07 12:48:33.347 [warning] could not find a pixi interpreter for the interpreter at /home/kamil/.pyenv/versions/3.10.8/envs/***
2024-08-07 12:48:33.357 [info] > ~/.pyenv/versions/3.10.8/envs/***/bin/python -m pytest -p vscode_pytest --collect-only .
2024-08-07 12:48:33.357 [info] cwd: .
2024-08-07 12:48:33.562 [error] Traceback (most recent call last):
  File "/home/kamil/.pyenv/versions/3.10.8/lib/python3.10/runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/home/kamil/.pyenv/versions/3.10.8/lib/python3.10/runpy.py", line 146, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/home/kamil/.pyenv/versions/3.10.8/lib/python3.10/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "/home/kamil/.pyenv/versions/3.10.8/envs/lab-analysing-pkgs/lib/python3.10/site-packages/pytest/__init__.py", line 9, in <module>
    from _pytest.assertion import register_assert_rewrite
  File "/home/kamil/.pyenv/versions/3.10.8/envs/lab-analysing-pkgs/lib/python3.10/site-packages/_pytest/assertion/__init__.py", line 11, in <module>
    from _pytest.assertion import rewrite
  File "/home/kamil/.pyenv/versions/3.10.8/envs/lab-analysing-pkgs/lib/python3.10/site-packages/_pytest/assertion/rewrite.py", line 32, in <module>
    from _pytest.assertion import util
  File "/home/kamil/.pyenv/versions/3.10.8/envs/lab-analysing-pkgs/lib/python3.10/site-packages/_pytest/assertion/util.py", line 24, in <module>
    from _pytest.config import Config
  File "/home/kamil/.pyenv/versions/3.10.8/envs/lab-analysing-pkgs/lib/python3.10/site-packages/_pytest/config/__init__.py", line 40, in <module>
    from pluggy import HookimplOpts
ImportError: cannot import name 'HookimplOpts' from 'pluggy' (/usr/lib/python3/dist-packages/pluggy/__init__.py)

2024-08-07 12:48:33.588 [error] Subprocess exited unsuccessfully with exit code 1 and signal null on workspace /home/kamil/***.
2024-08-07 12:48:33.589 [error] Subprocess exited unsuccessfully with exit code 1 and signal null on workspace /home/kamil/***. Creating and sending error discovery payload
2024-08-07 12:48:33.589 [error] pytest test discovery error for workspace:  /home/kamil/*** 
  
 The python test process was terminated before it could exit on its own, the process errored with: Code: 1, Signal: null for workspace /home/kamil/***

Extension version: 2024.12.2
VS Code version: Code 1.92.0 (b1c0a14de1414fcdaa400695b4db1c0799bc3124, 2024-07-31T23:26:45.634Z)
OS version: Linux x64 6.5.0-45-generic
Modes:

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


venvFolders: "<placeholder>"

languageServer: "Pylance"

testing
• pytestArgs: "<placeholder>"
• pytestEnabled: true

Installed Extensions
Extension Name Extension Id Version
Bookmarks alefragnani.Bookmarks 13.5.0
Code Spell Checker streetsidesoftware.code-spell-checker 3.0.1
Dev Containers ms-vscode-remote.remote-containers 0.380.0
EditorConfig for VS Code EditorConfig.EditorConfig 0.16.4
ESLint dbaeumer.vscode-eslint 3.0.10
Even Better TOML tamasfe.even-better-toml 0.19.2
GitHub Actions me-dutour-mathieu.vscode-github-actions 3.0.1
GitHub Markdown Preview bierner.github-markdown-preview 0.3.0
GitHub Pull Requests GitHub.vscode-pull-request-github 0.94.0
GitLens — Git supercharged eamodio.gitlens 15.2.3
Hex Editor ms-vscode.hexeditor 1.10.0
IntelliCode VisualStudioExptTeam.vscodeintellicode 1.3.1
JavaScript Debugger ms-vscode.js-debug 1.92.0
JavaScript Debugger Companion Extension ms-vscode.js-debug-companion 1.1.3
Jupyter ms-toolsai.jupyter 2024.7.0
Jupyter Cell Tags ms-toolsai.vscode-jupyter-cell-tags 0.1.9
Jupyter Keymap ms-toolsai.jupyter-keymap 1.1.2
Jupyter Notebook Renderers ms-toolsai.jupyter-renderers 1.0.19
Jupyter Slide Show ms-toolsai.vscode-jupyter-slideshow 0.1.6
Markdown All in One yzhang.markdown-all-in-one 3.6.2
Markdown Checkboxes bierner.markdown-checkbox 0.4.0
Markdown Emoji bierner.markdown-emoji 0.3.0
Markdown Footnotes bierner.markdown-footnotes 0.1.1
Prettier - Code formatter esbenp.prettier-vscode 10.4.0
Pylance ms-python.vscode-pylance 2024.7.1
Python ms-python.python 2024.12.1
Python Debugger ms-python.debugpy 2024.10.0
Python Test Explorer for Visual Studio Code littlefoxteam.vscode-python-test-adapter 0.8.2
Remote - SSH ms-vscode-remote.remote-ssh 0.112.0
Remote Explorer ms-vscode.remote-explorer 0.4.3
Ruff charliermarsh.ruff 2024.36.0
sidebar-markdown-notes assisrMatheus.sidebar-markdown-notes 1.2.0
SQLite alexcvzz.vscode-sqlite 0.14.1
ssort mikoz.ssort 1.0.2
Supermaven Supermaven.supermaven 1.0.8
Table Visualizer for JavaScript Profiles ms-vscode.vscode-js-profile-table 1.0.9
Terminal.env hzeineddine.terminal-dotenv 1.0.0
Test Adapter Converter ms-vscode.test-adapter-converter 0.1.9
Test Explorer UI hbenl.vscode-test-explorer 2.21.1
YAML redhat.vscode-yaml 1.15.0
A/B Experiments
vsliv368:30146709
vspor879:30202332
vspor708:30202333
vspor363:30204092
vscoreces:30445986
vscod805cf:30301675
binariesv615:30325510
vsaa593:30376534
py29gd2263:31024239
c4g48928:30535728
azure-dev_surveyone:30548225
2i9eh265:30646982
962ge761:30959799
pythongtdpath:30769146
welcomedialogc:30910334
pythonnoceb:30805159
asynctok:30898717
pythonregdiag2:30936856
pythonmypyd1:30879173
h48ei257:31000450
pythontbext0:30879054
accentitlementst:30995554
dsvsc016:30899300
dsvsc017:30899301
dsvsc018:30899302
cppperfnew:31000557
dsvsc020:30976470
pythonait:31006305
dsvsc021:30996838
0ee40948:31013168
pythoncenvpt:31062603
a69g1124:31058053
dvdeprecation:31068756
dwnewjupyter:31046869
newcmakeconfigv2:31071590
impr_priority:31102340
nativerepl2:31104044
refactort:31108082
ccpltc:31103427
pythonrstrctxtcf:31103194
wkspc-onlycs-t:31111718

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Aug 7, 2024
@eleanorjboyd
Copy link
Member

Hi! To clarify is only testing using the wrong environment or the extension as a whole? We are aware of an issue of incorrect environment selection for testing but if this is for the extension overall then it would be related to something else and I can loop another coworker in.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Oct 3, 2024
@kam193
Copy link
Author

kam193 commented Oct 3, 2024

In my experience, it's probably everything taking the python path from the Python extension: Jupyter Notebooks, Terminal (I had to override the PYTHONPATH in terminal's env settings). When trying to debug what is wrong with the Jupyter Notebooks, I managed to narrow the issue to environmental variables delivered by the Python extension.

Side note: today I surprisingly was able to use some features that a few days ago in a different project were broken. I still don't know if something was fixed (e.g. in the newest release), or in this particular project I just didn't have anything conflicting with the system packages.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Oct 3, 2024
@eleanorjboyd
Copy link
Member

Ah gotcha- so were you able to adjust the environment variables or is it still causing issues? We have make many improvements in this area (environment selection and activation) and so hopefully that was the fix that helped with the previously broken package

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Oct 3, 2024
@kam193
Copy link
Author

kam193 commented Oct 4, 2024

I will test it again and come back probably next week (I won't be able to do it today)

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Oct 4, 2024
@kam193
Copy link
Author

kam193 commented Oct 8, 2024

Hey, I've checked again, and unfortunately, it still does not work. Sometimes I have luck that what I need does not conflict with the system packages, but generally - the wrong PYTHONPATH is still here, everywhere.

@kam193
Copy link
Author

kam193 commented Oct 11, 2024

FYI: I've just found a new workaround, let's configure Python extension to use .env file and put there manually the expected PYTHONPATH, like:

PYTHONPATH=/home/[...]/.venv/lib/python3.10/site-packages

Then in the terminal you can observe:

$ env | grep PYTHON
PYTHONPATH=/home/[...]/.venv/lib/python3.10/site-packages:/usr/lib/python3/dist-packages

And at least the Jupyter started to work. Not that in this case I used a local venv instead of the one from pyenv (I hope it's not the requirement, just a thing I needed).

This is weird.

@kam193 kam193 closed this as completed Oct 11, 2024
@kam193
Copy link
Author

kam193 commented Oct 11, 2024

I didn't intend to close the issue, it's a missclick - could you please reopen it?

@eleanorjboyd eleanorjboyd reopened this Oct 29, 2024
@eleanorjboyd
Copy link
Member

sorry for the delay- will investigate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-testing triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

2 participants