Skip to content

Selected interpreter changes when the extension is starting up, when in DeprecatePythonPath exp #16291

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

Closed
sgargan opened this issue May 20, 2021 · 10 comments · Fixed by #16391
Closed
Labels
area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug important Issue identified as high-priority regression Bug didn't exist in a previous release

Comments

@sgargan
Copy link

sgargan commented May 20, 2021

Environment data

  • Language Server version: 2021.5.3 (pyright 1f57ad5c) starting
  • OS and version: OSX 11.2.3 Big Sur
  • Python version: 3.8.x installed via asdf

vscode

  • Version: 1.56.2
  • Commit: 054a9295330880ed74ceaedda236253b4f39a335
  • Date: 2021-05-12T17:44:30.902Z (1 wk ago)
  • Electron: 12.0.4
  • Chrome: 89.0.4389.114
  • Node.js: 14.16.0
  • V8: 8.9.255.24-electron.0
  • OS: Darwin x64 20.3.0

Expected behaviour

I expect vscode/python extension/pylance to respect the pythonPath from .vscode/settings.json and PYTHONPATH from .env when starting up the language server and running tools like the test discovery. Right now it ignores these and uses the python in the system path.

Actual behaviour

So I stumbled into this trying to get the test discovery working. Typically I work in a virtualenv for each project. I create a virtualenv in my project folder with the python3 module

python3 -m venv .
source bin/activate

I have source in a src folder, tests in tests and a requirements file installs pytest. Using the following runs my tests perfectly from the command line

PYTHONPATH=lib:tests:src pytest tests

To use this venv in vscode I setup the following .settings file

{
    "python.pythonPath": "<full_path_to_project>/bin/python",
    "python.envFile": "${workspaceFolder}/.env",
    "python.testing.pytestPath": "${workspaceFolder}/bin/pytest",
    "python.analysis.extraPaths": ["src", "tests"],
    "python.analysis.logLevel": "Trace",
    "python.testing.pytestEnabled": true
}

and the .env folder to specify the PYTHONPATH looks like

PYTHONPATH=./src:./tests:./lib

When vscode first starts I see it is using my virtualenv path

Screenshot 2021-05-20 at 14 07 47

Then soon as I click a python file e.g. a test file that kicks off the discovery, the language server boots and uses the first python it finds in the path, overriding my virtualenv one. See logs below

[Info  - 14:07:43] Setting pythonPath for service "pylance-issue": "/Users/sgargan/.asdf/installs/python/3.8.8/bin/python3.8"

Screenshot 2021-05-20 at 14 08 20

Of course this has none of my libraries installed and so the test discovery fails completely.

FWICT my vscode environment is setup correctly. if i prefix the ptyest discovery command with my PYTHONPATH and run it in a terminal, it discovers everything correctly. Not really sure why the

Logs

here are the pylance logs

[Info  - 14:07:43] Pylance language server 2021.5.3 (pyright 1f57ad5c) starting
[Info  - 14:07:43] Server root directory: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist
[Info  - 14:07:43] No configuration file found.
[Info  - 14:07:43] No pyproject.toml file found.
[Info  - 14:07:43] Setting pythonPath for service "pylance-issue": "/Users/sgargan/.asdf/installs/python/3.8.8/bin/python3.8"
[Warn  - 14:07:43] stubPath /Users/sgargan/devroot/secdev/pylance-issue/typings is not a valid directory.
[Info  - 14:07:43] Assuming Python version 3.8
[Info  - 14:07:43] Assuming Python platform Darwin
Search paths for /Users/sgargan/devroot/secdev/pylance-issue
  /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib
  /Users/sgargan/devroot/secdev/pylance-issue
  /Users/sgargan/devroot/secdev/pylance-issue/src
  /Users/sgargan/devroot/secdev/pylance-issue/tests
  /Users/sgargan/devroot/secdev/pylance-issue/src
  /Users/sgargan/devroot/secdev/pylance-issue/tests
  /Users/sgargan/devroot/secdev/pylance-issue/lib
  /Users/sgargan/devroot/secdev/pylance-issue/typings
  /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stubs/...
  /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/bundled/stubs
  /users/sgargan/.asdf/installs/python/3.8.8/lib/python3.8
  /users/sgargan/.asdf/installs/python/3.8.8/lib/python3.8/lib-dynload
  /users/sgargan/.local/lib/python3.8/site-packages
  /users/sgargan/.asdf/installs/python/3.8.8/lib/python3.8/site-packages
[Info  - 14:07:43] Searching for source files
[Info  - 14:07:43] Auto-excluding /Users/sgargan/devroot/secdev/pylance-issue
[Info  - 14:07:43] No source files found.
[FG] parsing: /Users/sgargan/devroot/secdev/pylance-issue/tests/simple_test.py.py (60ms)
[FG] parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 3ms] (99ms)
[FG] binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/builtins.pyi (36ms)
[FG] binding: /Users/sgargan/devroot/secdev/pylance-issue/tests/simple_test.py.py (1ms)
[Info  - 14:07:44] Background analysis(1) root directory: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist
[Info  - 14:07:44] Background analysis(1) started
Background analysis message: setConfigOptions
Background analysis message: ensurePartialStubPackages
Background analysis message: setTrackedFiles
Background analysis message: markAllFilesDirty
Background analysis message: setFileOpened
Background analysis message: setFileOpened
Background analysis message: analyze
[BG(1)] analyzing: /Users/sgargan/devroot/secdev/pylance-issue/tests/simple_test.py ...
[BG(1)]   parsing: /Users/sgargan/devroot/secdev/pylance-issue/tests/simple_test.py (35ms)
[BG(1)]   parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/builtins.pyi [fs read 2ms] (78ms)
[BG(1)]   binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/builtins.pyi (35ms)
[BG(1)]   binding: /Users/sgargan/devroot/secdev/pylance-issue/tests/simple_test.py (1ms)
[BG(1)]   checking: /Users/sgargan/devroot/secdev/pylance-issue/tests/simple_test.py ...
[BG(1)]     parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/typing.pyi [fs read 599ms] (638ms)
[BG(1)]     binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/typing.pyi (10ms)
[BG(1)]     parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi [fs read 1ms] (7ms)
[BG(1)]     binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/_typeshed/__init__.pyi (1ms)
[BG(1)]     parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/typing_extensions.pyi [fs read 0ms] (2ms)
[BG(1)]     binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/typing_extensions.pyi (1ms)
[BG(1)]     parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/__init__.pyi [fs read 0ms] (5ms)
[BG(1)]     binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/__init__.pyi ...
[BG(1)]       parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/async_case.pyi [fs read 0ms] (1ms)
[BG(1)]       binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/async_case.pyi (0ms)
[BG(1)]       parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/loader.pyi [fs read 0ms] (1ms)
[BG(1)]       binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/loader.pyi (1ms)
[BG(1)]       parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/main.pyi [fs read 0ms] (2ms)
[BG(1)]       binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/main.pyi (1ms)
[BG(1)]       parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/runner.pyi [fs read 0ms] (2ms)
[BG(1)]       binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/runner.pyi (1ms)
[BG(1)]       parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/signals.pyi [fs read 0ms] (1ms)
[BG(1)]       binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/signals.pyi (0ms)
[BG(1)]       parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/suite.pyi [fs read 1ms] (3ms)
[BG(1)]       binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/suite.pyi (1ms)
[BG(1)]     binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/__init__.pyi (16ms)
[BG(1)]     parsing: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/case.pyi [fs read 0ms] (10ms)
[BG(1)]     binding: /Users/sgargan/.vscode/extensions/ms-python.vscode-pylance-2021.5.3/dist/typeshed-fallback/stdlib/unittest/case.pyi (8ms)

@jakebailey
Copy link
Member

To confirm, are you saying that it starts with the correct environment selected at the bottom left, then switches to the wrong one when you open a file, and that's what your screenshots are showing?

Pylance uses the interpreter selected by the Python extension and doesn't go out to PATH. This sounds more like a Python extension interpreter selector bug than anything.

@sgargan
Copy link
Author

sgargan commented May 20, 2021

it starts with the right venv one and soon as the language server starts you can see it picks up the wrong one from the path.

soon as it gets this far

 Setting pythonPath for service "pylance-issue": "/Users/sgargan/.asdf/installs/python/3.8.8/bin/python3.8"

the toolbar updates and it's pointed to the wrong one.

@jakebailey
Copy link
Member

Thanks. I don't think this is a Pylance bug, then, but a bug in the Python extension to do with the interpreter selector. Pylance just queries for whichever interpreter is active and doesn't have the power to change it.

I'll transfer this over to them; you will probably want to provide the logs in the Python output window as well, as that should provide some info as to what's going on here.

@jakebailey jakebailey transferred this issue from microsoft/pylance-release May 20, 2021
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label May 20, 2021
@sgargan
Copy link
Author

sgargan commented May 24, 2021

@jakebailey, where can I track this transfer? will they reply here or are we talking a different project? thanks

@jakebailey
Copy link
Member

The issue has already been transferred, and the core extension team will handle this.

@karrtikr karrtikr added triage area-environments Features relating to handling interpreter environments and removed triage-needed Needs assignment to the proper sub-team labels May 24, 2021
@karrtikr
Copy link

Thanks for the bug report! We just wanted to quickly acknowledge we received it and we will triage this as soon as we can.

@karrtikr karrtikr removed their assignment May 24, 2021
@karrtikr
Copy link

karrtikr commented May 24, 2021

@sgargan Please provide the logs as mentioned in the issue template.

Logs

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

XXX

@karrtikr
Copy link

I do see that the Interpreter selected is not retained similar to #16291 when in deprecatePythonPath experiment. Can you add the following to your user settings and see if it helps,

"python.experiments.optOutFrom": ["DeprecatePythonPath - experiment"]

@karrtikr karrtikr self-assigned this May 25, 2021
@karrtikr karrtikr added info-needed Issue requires more information from poster investigating We are looking into the cause of the issue important Issue identified as high-priority needs PR regression Bug didn't exist in a previous release bug Issue identified by VS Code Team member as probable bug and removed triage info-needed Issue requires more information from poster investigating We are looking into the cause of the issue labels May 25, 2021
@sgargan
Copy link
Author

sgargan commented May 26, 2021

@karrtikr the opt out worked for me, my venv python doesn't change, the test discovery works and I can run/debug tests again. Thanks for your help here. LMK if the logs are still useful to you and I'll add them.

@karrtikr
Copy link

Great, thanks for confirming.

@karrtikr karrtikr removed the info-needed Issue requires more information from poster label May 26, 2021
@karrtikr karrtikr removed their assignment May 26, 2021
@karrtikr karrtikr changed the title Pylance ignores pythonPath & .env for virtual env Selected interpreter changes when the extension is starting up, when in DeprecatePythonPath exp May 26, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-environments Features relating to handling interpreter environments bug Issue identified by VS Code Team member as probable bug important Issue identified as high-priority regression Bug didn't exist in a previous release
Projects
None yet
4 participants