Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

PTH files support #537

Closed
d3r3kk opened this issue Jan 17, 2019 · 26 comments · Fixed by #1289
Closed

PTH files support #537

d3r3kk opened this issue Jan 17, 2019 · 26 comments · Fixed by #1289
Assignees
Labels
editable-installs pip editable installs / setup.py develop enhancement New feature or request feature: imports
Milestone

Comments

@d3r3kk
Copy link

d3r3kk commented Jan 17, 2019

@jaredcm commented on Mon Dec 17 2018

Environment data

  • VS Code version: Version 1.30.0 (1.30.0)
  • Extension version (available under the Extensions sidebar): 2018.12.1
  • OS and version: Mac OS 10.14.1
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7 cPython
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): pyenv
  • Relevant/affected Python packages and their versions: None

Expected behaviour

Adding custom locations to .pth file should be resolved by the Python intellisense.

Actual behaviour

I have a custom package that is in a subdirectory of my project. I want Python to resolve it as if it were an installed module.

The project is setup as such.
os_path
-|+- project_dir
--|+- bin
---|+- test.py
--|+- lib
---|+- custom_module
----|+- __init__.py

In the python 3.7 site-packages directory I have a .pth file with something like the following contents.
{os_dir}/{project_dir}
{os_dir}/{project_dir}/lib

In Python this resolves without issue and so I can do.

Python 3.7.0 (default, Aug 21 2018, 11:16:24) 
[Clang 9.1.0 (clang-902.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import custom_module
>>> custom_module.test()
Hello

If I create a test.py file in the project's bin directory like so:

"""This is a test."""
import custom_module
custom_module.test()

In VSCode I get unresolved import:

{
"resource": "/{os_path}/{project_dir}/bin/test.py",
"owner": "generated_diagnostic_collection_name#0",
"code": "unresolved-import",
"severity": 4,
"message": "unresolved import 'custom_module'",
"source": "Python",
"startLineNumber": 2,
"startColumn": 8,
"endLineNumber": 2,
"endColumn": 21
}

If I switch the import to lib.{custom_module} then the module resolution works. I'm guessing this is because VSCode adds the workspace to the PYTHONPATH.

Steps to reproduce:

  1. Setup a project with a module that is +1 level deep in the project. i.e. lib/custom_module/__init__.py
  2. Add a test function for resolution to __init__.py
"""This is a test."""


def test() -> None:
    """This is a test function."""
    print("Hello")

  1. In the Python site-packages create a .pth file and add the full OS path to the lib directory.
  2. Try to import the custom_module in a test file (see my test file above).

Logs

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

##########Linting Output - pydocstyle##########
##########Linting Output - mypy##########
##########Linting Output - prospector##########
##########Linting Output - prospector##########
##########Linting Output - pylama##########

Log (Window)

[2018-12-17 11:35:21.724] [renderer1] [error] Tree element not found: [object Object]: Error: Tree element not found: [object Object]
    at e.getElementLocation (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:214:266)
    at e.isCollapsed (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:213:757)
    at i.e.isCollapsed (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:962:723)
    at i.e.isExpanded (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:962:786)
    at t.revealMarkersForCurrentActiveEditor (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4710:827)
    at t.autoReveal (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4710:647)
    at t.onActiveEditorChanged (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4707:826)
    at e.fire (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:189:532)
    at t.doEmitActiveEditorChangeEvent (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4720:942)
    at t.handleActiveEditorChange (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4720:718)
    at file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4721:55
    at e.fire (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:189:512)
    at file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:5170:861
    at Object.g [as _notify] (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:167:807)
    at Object.enter (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:171:85)
    at n.Class.derive._creator._run (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:172:932)
    at n.Class.derive._creator._completed (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:172:373)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)

log.ts:169   ERR Tree element not found: [object Object]: Error: Tree element not found: [object Object]
    at e.getElementLocation (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:214:266)
    at e.isCollapsed (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:213:757)
    at i.e.isCollapsed (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:962:723)
    at i.e.isExpanded (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:962:786)
    at t.revealMarkersForCurrentActiveEditor (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4710:827)
    at t.autoReveal (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4710:647)
    at t.onActiveEditorChanged (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4707:826)
    at e.fire (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:189:532)
    at t.doEmitActiveEditorChangeEvent (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4720:942)
    at t.handleActiveEditorChange (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4720:718)
    at file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4721:55
    at e.fire (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:189:512)
    at file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:5170:861
    at Object.g [as _notify] (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:167:807)
    at Object.enter (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:171:85)
    at n.Class.derive._creator._run (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:172:932)
    at n.Class.derive._creator._completed (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:172:373)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
console.ts:134 [Extension Host] Python Extension: Failed to parse Prospector output SyntaxError: Unexpected end of JSON input
	at JSON.parse (<anonymous>)
	at module.exports.t.Prospector.<anonymous> (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:585133)
	at Generator.next (<anonymous>)
	at module.exports.r (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:584675)
	at new Promise (<anonymous>)
	at module.exports.r (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:584452)
	at module.exports.t.Prospector.parseMessages (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:585083)
	at module.exports.t.Prospector.<anonymous> (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:1:77469)
	at Generator.next (<anonymous>)
	at s (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:1:75269)
	at <anonymous>
t.log @ console.ts:134
t._logExtensionHostMessage @ extensionHost.ts:453
(anonymous) @ extensionHost.ts:244
emitTwo @ events.js:126
emit @ events.js:214
emit @ internal/child_process.js:772
_combinedTickCallback @ internal/process/next_tick.js:141
_tickCallback @ internal/process/next_tick.js:180

@jaredcm commented on Mon Dec 17 2018

I tried to create a brand new project and recreate this. The custom module is resolved without issue. When I try and import modules from my initial project I still cannot resolve them. In both projects I get an error message in the workload console like:

/Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:303 [Extension Host] Python Extension: Failed to parse Prospector output SyntaxError: Unexpected end of JSON input
	at JSON.parse (<anonymous>)
	at module.exports.t.Prospector.<anonymous> (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:585133)
	at Generator.next (<anonymous>)
	at module.exports.r (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:584675)
	at new Promise (<anonymous>)
	at module.exports.r (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:584452)
	at module.exports.t.Prospector.parseMessages (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:585083)
	at module.exports.t.Prospector.<anonymous> (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:1:77469)
	at Generator.next (<anonymous>)
	at s (/Users/jared/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:1:75269)
	at <anonymous>
t.log @ /Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:303

@jaredcm commented on Mon Dec 17 2018

Found the issue. Looks to be a corrupted .mypy_cache. I removed the cache directory from my initial project and the imports started working in both projects.


@jaredcm commented on Mon Dec 17 2018

Looks like I was wrong. I restarted my computer and now my custom module cannot be resolved again. I remember I had been messing about with my pth file. Normally I have 4 paths in it which are all loaded into Python without issue. While filling out this bug report, I had removed all but 1, which was when VS Code started working. I had to add the other 3 paths back again later for my scripts to function properly. Which is why it stopped working and I didn't notice until VSCode was restarted.

project.pth =
os_path/project
os_path/project/lib
os_path/project/tests
os_path/project/unit_tests

I've done some testing:

  1. I added a project_lib.pth file which has 1 entry pointing to my lib directory and removed the entry from my project.pth file. The custom module is not resolvable.
  2. I change the name of project.pth to project.orig and restart VSCode. The module is resolvable.
  3. I change the name of project.orig back to project.pth and restart VSCode. The module is unresolvable again.
  4. I rename project.pth again, create a new file named project_root.pth, add the root directory for my project and restart VSCode. The module is resolvable as are modules in my project's root directory.

It looks like VSCode gets confused when pth files have more than one entry.


@jaredcm commented on Mon Dec 17 2018

New issue:
In custom_module I have many sub_modules and they have other sub_modules. Within each of the sub_modules I use relative imports to load their child classes. The sub_module directory has a __init__,py with the import statement of "from .rest import Rest". This should resolve to a child directory named 'rest' that has a __init__.py file where the Rest class is defined. The full resolution from the root module to the imported class is 'custom_module.sub_module.rest.Rest'

The error message is:
{
"resource": "~/project/lib/custom_module/sub_module/__init__.py",
"owner": "generated_diagnostic_collection_name#0",
"code": "unresolved-import",
"severity": 4,
"message": "unresolved import 'lib.custom_modulerest'",
"source": "Python",
"startLineNumber": 6,
"startColumn": 6,
"endLineNumber": 6,
"endColumn": 11
}

Also notice that the message is not showing the correct module import path. The lib directory is not a module as it does not have a __init__.py file. If I change the path of the relative import to a full module import of 'from custom_module.sub_module.rest import Rest', then the error goes away. The path in the message is also missing the current module. It's almost like it the path it is looking for is shifted too far up the tree. Instead of looking in custom_module/sub_module/rest, I think it is looking for lib/custom_module/rest which doesn't exist.

Developer Console output

  ERR Tree element not found: [object Object]: Error: Tree element not found: [object Object]
    at e.getElementLocation (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:214:266)
    at e.isCollapsed (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:213:757)
    at i.e.isCollapsed (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:962:723)
    at i.e.isExpanded (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:962:786)
    at t.revealMarkersForCurrentActiveEditor (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4710:827)
    at t.autoReveal (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4710:647)
    at t.onActiveEditorChanged (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4707:826)
    at e.fire (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:189:532)
    at t.doEmitActiveEditorChangeEvent (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4720:942)
    at t.handleActiveEditorChange (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4720:718)
    at file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:4721:55
    at e.fire (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:189:512)
    at file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:5170:861
    at Object.g [as _notify] (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:167:807)
    at Object.enter (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:171:85)
    at n.Class.derive._creator._run (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:172:932)
    at n.Class.derive._creator._completed (file:///Applications/Design/Visual Studio Code.app/Contents/Resources/app/out/vs/workbench/workbench.main.js:172:373)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
console.ts:134 [Extension Host] Python Extension: Failed to parse Prospector output SyntaxError: Unexpected end of JSON input
	at JSON.parse (<anonymous>)
	at module.exports.t.Prospector.<anonymous> (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:585133)
	at Generator.next (<anonymous>)
	at module.exports.r (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:584675)
	at new Promise (<anonymous>)
	at module.exports.r (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:584452)
	at module.exports.t.Prospector.parseMessages (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:83:585083)
	at module.exports.t.Prospector.<anonymous> (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:1:77469)
	at Generator.next (<anonymous>)
	at s (/~/.vscode/extensions/ms-python.python-2018.12.1/out/client/extension.js:1:75269)
	at <anonymous>
t.log @ console.ts:134
t._logExtensionHostMessage @ extensionHost.ts:453
(anonymous) @ extensionHost.ts:244
emitTwo @ events.js:126
emit @ events.js:214
emit @ internal/child_process.js:772
_combinedTickCallback @ internal/process/next_tick.js:141
_tickCallback @ internal/process/next_tick.js:180

@d3r3kk commented on Thu Jan 17 2019

Thanks for bringing this up @jaredcm, an interesting problem!

I didn't know about .pth files until this issue popped up so thank you for that.

I can repro this issue with the latest stable VS Code (1.30.2) + latest extension (2018.12.1), using the Jedi language server ("python.jediEnabled": true").

Workaround: The Microsoft Python Language Server (as of version 0.1.72.0, or greater) seems to handle this case (see below). Have you attempted to use this instead of Jedi? See this issue for details on how to enable/update the Microsoft Python Language Server.

Adding more than one entry.

Actually, on second look I have found that this is not handled properly by the Microsoft Python Language Server as I'd originally stated (at least version 0.1.72.0 doesn't) as I'd left out the multiple lines in the .pth part after I got things up and running.

Indeed, if you add a .pth file to your site-packages with > 1 path, the issue does repro.

Here's the sample workspace:

3727_module_resolution.zip

Here's the sample .pth file contents that I placed in the site-packages. I created a .venv in my workspace and copied this file to ${workspaceFolder}/.venv/Lib/site-packages/test_3727.pth.

C:\\path\\to\\workspaceFolder\\3727_module_resolution
C:\\path\\to\\workspaceFolder\\3727_module_resolution\\lib
@d3r3kk
Copy link
Author

d3r3kk commented Jan 17, 2019

Additional information, gif of my repro:

3727_module_reso_with_pth

@MikhailArkhipov
Copy link

To verify with the new LS

@qubitron qubitron added this to the Feb 2019.2 milestone Feb 20, 2019
@tejajrk
Copy link

tejajrk commented Mar 1, 2019

adding a path file in the site-packages is a workaround. But language server should be able to recognize the package through init.py. Hopefully this is going to be added.

@jaredcm
Copy link

jaredcm commented Mar 1, 2019

PTH files are for the module to be resolvable regardless of where it is executed. While also not having to manage PYTHONPATH. It is a perfect solution for working on a standalone module nested inside a repo's other stuff that is relied on by other unaffiliated code.

@MikhailArkhipov
Copy link

OK, this is about Adding custom locations to .pth file should be resolved by the Python intellisense. This is an enhancement and not a bug. LS has no knowledge of those files at the moment.

@MikhailArkhipov MikhailArkhipov changed the title Python file with more than one entry breaks module resolution PTH files support Mar 3, 2019
@jaredcm
Copy link

jaredcm commented Mar 3, 2019

I started using a single PTH file with multiple paths in about August. It was working in CPython and in VS Code. In about November or December something changed and VS Code stopped resolving my module (although CPython is still able to resolve them). I finally found the workaround as specified in the defect. Use 1 PTH file for each path you want to resolve, then both systems work just fine. Relative paths inside the module stopped resolving in VS Code around the same time and still do not resolve, which is a different problem.

https://docs.python.org/3/library/site.html

A path configuration file is a file whose name has the form name.pth and exists in one of the four directories mentioned above; its contents are additional items (one per line) to be added to sys.path. Non-existing items are never added to sys.path, and no check is made that the item refers to a directory rather than a file. No item is added to sys.path more than once. Blank lines and lines beginning with # are skipped. Lines starting with import (followed by space or tab) are executed.

@MikhailArkhipov MikhailArkhipov removed this from the Feb 2019.2 milestone Mar 3, 2019
@MikhailArkhipov
Copy link

@jaredcm - was it LS or Jedi

@jaredcm
Copy link

jaredcm commented Mar 3, 2019

I've been using LS exclusively from when I started using VS Code.

@jakebailey
Copy link
Member

We don't explicitly support pth files yet, but note that if you have a different directory which contains your code (like my_package), then our recommendation is to configure the workspace to point the LS at that directory. See: https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings

This should fix the original issue reported here, given the screenshots. LS version 0.3.1+ should fix the issue with editable installs (which pth files are used in), and is available in the daily channel by setting:

"python.analysis.downloadChannel": "daily"

@jaredcm
Copy link

jaredcm commented Jun 14, 2019

Thanks for the update. The original problem was that PTH files with multiple entries stopped working. I resolved my original problem by putting 1 path per PTH file. LanguageServer seemed okay with it and has been working fine for the past 6 months. I put a comment in #1171 that language server is hanging when pth files are pointing to the same directories as the workspace. I just tried the daily (0.3.12) it still hangs. I'm unsure if this recent breakage is due to any fixes as part of the defect.

My PTH configuration works in Python REPL, IPython with all of the Python linting tools, and VSCode with Jedi also works. I switched to Jedi after hitting #1171 a week or so ago. I tried switching back to LanguageServer today because of an unrelated issue in Jedi davidhalter/jedi#1299.

/Users/{user}/Documents/Code/stress_test is the root of my workspace.
/Users/{user}/Documents/Code/stress_test/lib is the root of my custom modules

Starting Microsoft Python language server.
Downloading https://pvsc.azureedge.net/python-language-server-daily/Python-Language-Server-osx-x64.0.3.12.nupkg... complete
Unpacking archive... done
[Info - 4:53:26 PM] Analysis cache path: /Users/{user}/Library/Caches/Microsoft/Python Language Server
[Info - 4:53:26 PM] GetCurrentSearchPaths /Users/{user}/.pyenv/versions/3.7.2/bin/python
[Info - 4:53:26 PM] Interpreter search paths:
[Info - 4:53:26 PM] /private/var/folders/9q/mshp41rj5419tdv0ftt4bz380000gn/T/cv3s5awh.asj
[Info - 4:53:26 PM] /Users/{user}/.pyenv/versions/3.7.2/lib/python3.7
[Info - 4:53:26 PM] /Users/{user}/.pyenv/versions/3.7.2/lib/python3.7/lib-dynload
[Info - 4:53:26 PM] /Users/{user}/.local/lib/python3.7/site-packages
[Info - 4:53:26 PM] /Users/{user}/.pyenv/versions/3.7.2/lib/python3.7/site-packages
[Info - 4:53:26 PM] /Users/{user}/Documents/Code/stress_test/cam_tests
[Info - 4:53:26 PM] /Users/{user}/Documents/Code/stress_test/lib
[Info - 4:53:26 PM] /Users/{user}/Documents/Code/stress_test
[Info - 4:53:26 PM] User search paths:
[Info - 4:53:27 PM] Microsoft Python Language Server version 0.3.12.0
[Info - 4:53:27 PM] Initializing for /Users/{user}/.pyenv/versions/3.7.2/bin/python
Opening document file:///Users/{user}/Documents/Code/pycam_stress_test/lib/py_mod/cli/commands/info_cmd.py
Analysis of lib.py_cam.cli.commands.info_cmd(Library) queued
Hover in file:///Users/{user}/Documents/Code/stress_test/lib/py_mod/cli/commands/info_cmd.py at (18, 15)

@jakebailey
Copy link
Member

So long as you set python.autoComplete.extraPaths with your import roots, things should work. In your case, that would be:

"python.autoComplete.extraPaths": ["./lib"]

As per https://github.com/microsoft/python-language-server/blob/master/TROUBLESHOOTING.md#unresolved-import-warnings.

@jaredcm
Copy link

jaredcm commented Jun 17, 2019

Ahhh, that worked for the majority of paths, thank you very much. Is there a way to surface this error, for better end-user understanding? I wasn't getting any warnings just a hung language server. I see now that the analyzed module is reporting the expected import path. I also see that when using extra paths the PTH paths are removed from system and put into user. I do have one more issue. I have a package in the root of the git repo/VS workspace which cannot be resolved, even if I put '.' or the full path in the setting. For this one I do actually get the unresolved-import-warning and language server does analyze the rest of the packages.

[Info - 2:45:04 PM] Interpreter search paths:
[Info - 2:45:04 PM] /private/var/folders/9q/mshp41rj5419tdv0ftt4bz380000gn/T/zfayqlky.aw5
[Info - 2:45:04 PM] /Users/{user}/.pyenv/versions/3.7.2/lib/python3.7
[Info - 2:45:04 PM] /Users/{user}/.pyenv/versions/3.7.2/lib/python3.7/lib-dynload
[Info - 2:45:04 PM] /Users/{user}/.local/lib/python3.7/site-packages
[Info - 2:45:04 PM] /Users/{user}/.pyenv/versions/3.7.2/lib/python3.7/site-packages
[Info - 2:45:04 PM] /Users/{user}/Documents/Code/pymod_stress_test <- git repo root
[Info - 2:45:04 PM] User search paths:
[Info - 2:45:04 PM] /Users/{user}/Documents/Code/pymod_stress_test/lib
[Info - 2:45:04 PM] /Users/{user}/Documents/Code/pymod_stress_test/mod_tests

Could it be the duplicate in the Interpreter search path?

@jakebailey
Copy link
Member

We're working on a potential way to surface the misconfiguration (or some auto config), but it's not a simple task to recognize the difference between "you just need this config" with "you genuinely have a bad import". From the analysis' point of view, all it knows is that it went to find a module and it wasn't there. Anything past that is going to have to be heuristics.

The workspace root should be removed from the interpreter path list, yes. I'm going to patch that now.

@jakebailey
Copy link
Member

This actually fixes a couple of other bugs; thank you for showing your logs! 😄

@jaredcm
Copy link

jaredcm commented Jun 17, 2019

Ok, thanks. Couldn't it be that anything in the interpreter paths with a common VSCode workspace path be moved to user?

@jakebailey
Copy link
Member

Can you be more specific? Many (perhaps most) users will do something like python -m venv venv in the same place as their code, so just treating everything that's in the workspace as user code doesn't work out.

@jaredcm
Copy link

jaredcm commented Jun 17, 2019

Oh, okay. I hadn't thought of the interpreter being in the workspace. Then I guess surfacing an error is the best recourse. What was throwing me was, as mentioned above, I wasn't getting import errors, language server was just hanging, so the extraPaths link didn't appear on the surface to be relevant. I don't know if one is already available but, a screencast on how language server works and how to debug it would be useful as well. Thanks for the help.

@jakebailey
Copy link
Member

The hanging is a bug, so I wouldn't worry about that specifically (I didn't realize the connection until #1175 had a repro), but working on better displaying what we expect to be configured (and eliminating that) is something to work on, yes.

@jaredcm
Copy link

jaredcm commented Jun 17, 2019

As part of testing your fix I switched my PTH configurator to multi-path in a single file, like I had it when I originally reported this defect. It works now with the extraPaths setting.

lh: pymod_stress_test > cat /Users/{user}/.pyenv/versions/3.7.2/lib/python3.7/site-packages/pymod_stress_test_root.pth 
/Users/{user}/Documents/Code/pymod_stress_test
/Users/{user}/Documents/Code/pymod_stress_test/lib
/Users/{user}/Documents/Code/pymod_stress_test/mod_tests
In [2]: import sys
In [3]: sys.path
Out[3]: 
['/Users/{user}/Documents/Code/pymod_stress_test/bin',
 '/Users/{user}/.pyenv/versions/3.7.2/lib/python37.zip',
 '/Users/{user}/.pyenv/versions/3.7.2/lib/python3.7',
 '/Users/{user}/.pyenv/versions/3.7.2/lib/python3.7/lib-dynload',
 '/Users/{user}/.local/lib/python3.7/site-packages',
 '/Users/{user}/.pyenv/versions/3.7.2/lib/python3.7/site-packages',
 '/Users/{user}/Documents/Code/pymod_stress_test',
 '/Users/{user}/Documents/Code/pymod_stress_test/lib',
 '/Users/{user}/Documents/Code/pymod_stress_test/mod_tests',
 '/Users/{user}/.pyenv/versions/3.7.2/lib/python3.7/site-packages/IPython/extensions']

Everything in VS seems to resolve properly and my intellisense has the correct information for the import including decorated properties that return on-demand instantiated classes. I think we can close this ticket. Thank you once again.

@jakebailey
Copy link
Member

I'm going to leave this open for now (if that's alright); I think we're using this issue to track specifically supporting reading the pth files themselves for info (which we currently do not do).

I'd be really curious as to why multiple pth files would make a difference; but I know that the site module does something special with the names of pth files internally which could have some effect, but I have not yet done that research to a large extent.

@jaredcm
Copy link

jaredcm commented Jun 17, 2019

Oh, that is fine. I don't know why there was a difference, either. Originally I was using multiple paths per file and it worked in LS. Then LS stopped resolving my packages, thus this defect. The workaround was to use 1 path per file and then LS was able to resolve again. Then we had the hang, and the extraPaths setting fixed that.

@leotohill
Copy link

I can repro additional problems when a .pth file has multiple lines, running Python 3.7.3 and LS 0.3.6.1 on Windows. From the preceding thread it's not clear whether my scenario is supported, so please tell me if I should open an issue. My variation is that I'm using a venv - my problems only occur when selecting it.

One of the symptoms is that if my .pth file has two lines, the first one being blank (empty), or commented with #, LS will not load: upon selecting the venv I see "! Select Python Interpreter" on the status line.

Another symptom is that if the .pth contains two valid lines, I get "unresolved reference" on "import sys".

I can also repro a "unresolved reference" problem with a single-line .pth file. This scenario is a bit different so I'll hold its details for now.

Please advise what you need.

@jakebailey
Copy link
Member

Interpreter selection ("Select Python Interpreter") is a VS Code Python extension issue. The language server only uses the interpreter that the extension says to use. If one isn't selected, then we will likely fail to find any stdlib stuff (including sys), since we won't where the python install/binary is. If you can't select an interpreter, then I'd suggest opening an issue on the extension: https://github.com/Microsoft/vscode-python

As for pth files, we don't actually read the pth files ourselves; we call out to Python to ask what its sys.path is set to, then use that for import resolution. Theoretically, a pth file having some number of lines should not be impactful so long as python can understand it.

@c4urself
Copy link

c4urself commented Jul 24, 2020

so a problem that I've run into when using a PTH file within my virtualenv's site-packages directory is that the output is parsed incorrectly by vscode because the python binary outputs additional lines to stdout with the current site-packages setup:

Python interpreter path: ./bazel-bin/robots/venv/bin/python
Error 2020-07-24 14:44:07: Failed to get interpreter information for '/Users/c4urself/code/adtcode/bazel-bin/robots/venv/bin/python' [Error: python /Users/c4urself/.vscode/extensions/ms-python.python-2020.7.96456/pythonFiles/pyvsc-run-isolated.py,/Users/c4urself/.vscode/extensions/ms-python.python-2020.7.96456/pythonFiles/interpreterInfo.py returned bad JSON (/Users/c4urself/code/adtcode/bazel-bin/robots/venv/lib/python3.7/site-packages
/Users/c4urself/code/adtcode/bazel-bin/robots/venv/lib/python3.7/site-packages/easy-install.pth
/Users/c4urself/code/adtcode/bazel-bin/robots/venv/lib/python3.7/site-packages
/Users/c4urself/code/adtcode/bazel-bin/robots/venv/lib/python3.7/site-packages/easy-install.pth
{"versionInfo": [3, 7, 4, "final", 0], "sysPrefix": "/Users/c4urself/code/adtcode/bazel-bin/robots/venv", "version": "3.7.4 (default, Nov  6 2019, 11:16:47) \n[Clang 11.0.0 (clang-1100.0.33.12)]", "is64Bit": true}
) (SyntaxError: Unexpected token / in JSON at position 0)
	at /Users/c4urself/.vscode/extensions/ms-python.python-2020.7.96456/out/client/extension.js:1:210080
	at Object.t.getInterpreterInfo (/Users/c4urself/.vscode/extensions/ms-python.python-2020.7.96456/out/client/extension.js:24:94166)
	at async c.getInterpreterInformationImpl (/Users/c4urself/.vscode/extensions/ms-python.python-2020.7.96456/out/client/extension.js:46:385997)
	at async c.getInterpreterInformation (/Users/c4urself/.vscode/extensions/ms-python.python-2020.7.96456/out/client/extension.js:46:385502)
	at async Promise.all (index 0)
	at async g.sendTelemetry (/Users/c4urself/.vscode/extensions/ms-python.python-2020.7.96456/out/client/extension.js:48:812348)]

You can see these extra lines rather simply:

 🌸  c4urself@eos ~ → cd /tmp/
 🌸  c4urself@eos /tmp → python3 -m venv testvenv
 🌸  c4urself@eos /tmp → touch testvenv/lib/python3.7/site-packages/test.pth
 🌸  c4urself@eos /tmp → ./testvenv/bin/python
/private/tmp/testvenv/lib/python3.7/site-packages
/private/tmp/testvenv/lib/python3.7/site-packages/test.pth
/private/tmp/testvenv/lib/python3.7/site-packages
/private/tmp/testvenv/lib/python3.7/site-packages/test.pth
Python 3.7.4 (default, Nov  6 2019, 11:16:47)
[Clang 11.0.0 (clang-1100.0.33.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
 🌸  c4urself@eos /tmp → rm testvenv/lib/python3.7/site-packages/test.pth
 🌸  c4urself@eos /tmp → ./testvenv/bin/python
Python 3.7.4 (default, Nov  6 2019, 11:16:47)
[Clang 11.0.0 (clang-1100.0.33.12)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

If the vscode extension can ignore these lines than .PTH files should run just fine as they just affect the sys.path on startup

@c4urself
Copy link

@jakebailey not sure if we can ignore those stdout lines, if we could that would be extremely helpful

@jakebailey
Copy link
Member

jakebailey commented Jul 24, 2020

This is the language server repo, but the errors above appear to be errors from the Python extension. If you can't activate your environment due to that I would open a bug with the reproducer there: https://github.com/microsoft/vscode-python

Though I'm not sure how much can be done if we the Python executable itself isn't doing what it's instructed due to a misconfiguration...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
editable-installs pip editable installs / setup.py develop enhancement New feature or request feature: imports
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants