Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 9c0ae95

Browse files
authored
Fix editable installs when using extraPaths/PYTHONPATH (microsoft#1183)
* Allow interpreter paths to be reclassified as user code if specified in user search paths * log raw search path results only in verbose mode * update TROUBLESHOOTING
1 parent d22adab commit 9c0ae95

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

TROUBLESHOOTING.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,10 @@ in [Filing an issue](#filing-an-issue).
99

1010
There are a few known issues in the current version of the language server:
1111

12-
- Import statement handling may be too strict, leading to "unresolved import" messages and the lack of analysis.
13-
- The language server considers the workspace root to be the root of user code imports. For the most part, this can be modified by adding additional folders to the `python.autoComplete.extraPaths` setting, for example, `"python.autoComplete.extraPaths": ["./src"]`, if `src` contains the user code. A `.env` file with `PYTHONPATH` set may also help.
14-
- Editable installs (`pip install -e` or `setup.py develop`) are known not to work with `extraPaths` when the package is installed. (#1139, #1013, #1137, #989, others).
1512
- Not all `__all__` statements can be handled.
1613
- Some modules may have an incorrect list of exported names.
1714
See [#620](https://github.com/Microsoft/python-language-server/issues/620),
1815
[#619](https://github.com/Microsoft/python-language-server/issues/619).
19-
- Persistent issues with high memory consumption for users.
20-
- In some contexts, users are experiencing higher than average amounts of memory being consumed. See [#832](https://github.com/Microsoft/python-language-server/issues/832).
2116

2217

2318
## Requirements
@@ -38,6 +33,47 @@ but may require outside libraries such as OpenSSL 1.0 or `libicu` on Linux.
3833

3934
## Common questions and issues
4035

36+
### Unresolved import warnings
37+
38+
If you're getting a warning about an unresolved import, first ensure that the
39+
package is installed into your environment if it is a library (`pip`, `pipenv`, etc).
40+
If the warning is about importing _your own_ code (and not a library), continue reading.
41+
42+
The language server treats the workspace root (i.e. folder you have opened) as
43+
the main root of user module imports. This means that if your imports are not relative
44+
to this path, the language server will not be able to find them. This is common
45+
for users who have a `src` directory which contains their code, a directory for
46+
an installable package, etc.
47+
48+
These extra roots must be specified to the language server. The easiest way to
49+
do this (with the VS Code Python extension) is to create a workspace configuration
50+
which sets `python.autoComplete.extraPaths`. For example, if a project uses a
51+
`src` directory, then create a file `.vscode/settings.json` in the workspace
52+
with the contents:
53+
54+
55+
```json
56+
{
57+
"python.autoComplete.extraPaths": ["./src"]
58+
}
59+
```
60+
61+
This list can be extended to other paths within the workspace (or even with
62+
code outside the workspace in more complicated setups). Relative paths will
63+
be taken as relative to the workspace root.
64+
65+
This list may also be configured using the `PYTHONPATH` environment variable,
66+
either set directly, or via a `.env` file in the workspace root (if using the
67+
Python extension):
68+
69+
```
70+
PYTHONPATH=./src
71+
```
72+
73+
For more examples, see issues:
74+
[#1085](https://github.com/microsoft/python-language-server/issues/1085#issuecomment-492919382),
75+
[#1169](https://github.com/microsoft/python-language-server/issues/1169#issuecomment-499998928)
76+
4177
### "Server initialization failed"
4278

4379
If you see this message, ensure that an interpreter has been selected. (See [Requirements](#requirements)).

src/Analysis/Ast/Impl/Modules/Resolution/MainModuleResolution.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ public async Task<IReadOnlyList<string>> GetSearchPathsAsync(CancellationToken c
7171
Debug.Assert(_searchPaths != null, "Should have search paths");
7272
_searchPaths = _searchPaths ?? Array.Empty<string>();
7373

74-
_log?.Log(TraceEventType.Information, "Python search paths:");
74+
_log?.Log(TraceEventType.Verbose, "Python search paths:");
7575
foreach (var s in _searchPaths) {
76-
_log?.Log(TraceEventType.Information, $" {s}");
76+
_log?.Log(TraceEventType.Verbose, $" {s}");
7777
}
7878

7979
var configurationSearchPaths = Configuration.SearchPaths ?? Array.Empty<string>();
8080

81-
_log?.Log(TraceEventType.Information, "Configuration search paths:");
81+
_log?.Log(TraceEventType.Verbose, "Configuration search paths:");
8282
foreach (var s in configurationSearchPaths) {
83-
_log?.Log(TraceEventType.Information, $" {s}");
83+
_log?.Log(TraceEventType.Verbose, $" {s}");
8484
}
8585
return _searchPaths;
8686
}
@@ -213,14 +213,25 @@ public async Task ReloadAsync(CancellationToken cancellationToken = default) {
213213

214214
InterpreterPaths = await GetSearchPathsAsync(cancellationToken);
215215

216-
var userSearchPaths = _interpreter.Configuration.SearchPaths.Except(InterpreterPaths, StringExtensions.PathsStringComparer);
216+
IEnumerable<string> userSearchPaths = Configuration.SearchPaths;
217+
InterpreterPaths = InterpreterPaths.Except(userSearchPaths, StringExtensions.PathsStringComparer);
217218

218219
if (Root != null) {
219220
var underRoot = userSearchPaths.ToLookup(p => _fs.IsPathUnderRoot(Root, p));
220221
userSearchPaths = underRoot[true];
221222
InterpreterPaths = underRoot[false].Concat(InterpreterPaths);
222223
}
223224

225+
_log?.Log(TraceEventType.Information, "Interpreter search paths:");
226+
foreach (var s in InterpreterPaths) {
227+
_log?.Log(TraceEventType.Information, $" {s}");
228+
}
229+
230+
_log?.Log(TraceEventType.Information, "User search paths:");
231+
foreach (var s in userSearchPaths) {
232+
_log?.Log(TraceEventType.Information, $" {s}");
233+
}
234+
224235
addedRoots.UnionWith(PathResolver.SetInterpreterSearchPaths(InterpreterPaths));
225236
addedRoots.UnionWith(SetUserSearchPaths(userSearchPaths));
226237
ReloadModulePaths(addedRoots);

0 commit comments

Comments
 (0)