question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jakebaileycommented, May 20, 2021

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.

0reactions
karrtikrcommented, May 26, 2021

Great, thanks for confirming.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Extension forgets the Python interpreter for my workspace ...
pythonPath" setting in your settings.json is no longer used by the Python extension. If you want, you can use a new setting called...
Read more >
Python in Visual Studio Code – May 2020 Release
One change that is coming relates to how the Python extension handles Python interpreter selection. Currently the path to the selected ...
Read more >
in Windows 10, How to configure Visual Studio Code to find ...
Go to File/Preferences/Settings and find Python Configuration . This is where you manually change ...
Read more >
Settings Reference for Python - Visual Studio Code
Note: Changes to this setting made after an interpreter has been selected for a workspace will not be applied or considered by the...
Read more >
What's New In Python 3.7 — Python 3.11.1 documentation
PEP 539 changes this by providing a new Thread Specific Storage (TSS) API to CPython which supersedes use of the existing TLS API...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found