Pylance-release: python.analysis.extraPaths setting supported?

Created on 2 Jul 2020  路  31Comments  路  Source: microsoft/pylance-release

Hi. Pylance looks great.

Is the "python.analysis.extraPaths" setting in settings.json supported by the pylance extension? I can't seem to get it working and wanted to check it is actually used by pylance. Or is there another way to tell pylance about other paths to search for imports? Does pylance make use of the pyrightconfig.json file for example?

Thanks.

More info...

I have some pypi packages downloaded in a separate directory called vendor and I'm trying to get pylance to search this extra directory.

More specifically, I have the directory /home/myusername/myrepo/vendor/py_deps/pypi_beautifulsoup4/ which contains the py4 beautiful soup package downloaded from pypi.

I can successfully run my python program by adding /home/myusername/myrepo/vendor/py_deps/pypi_beautifulsoup4/ to PYTHONPATH, as an import such as import py4 is then resolved correctly.

However with pylance I get a 'reportMissingImports error'. I've tried adding the path to my vendor/pydeps/pypi_beautifulsoup4 directory to "python.analysis.extraPaths" but I get the same error and can't tell if pylance is using this particular setting.

# /home/myusername/myrepo/src/somepackage/main.py

import bs4 # Import "bs4" could not be resolved Pylance (reportMissingImports)

def main():
    pass
needs documentation

Most helpful comment

Thanks for the feedback.

The setting is supported.

I'm not sure what is happening in @mikelnrd 's case, but for @htarnacki it looks like the issue is that we don't support replacing variables such as $(workspaceFolder}. Since the path is relative to the workspace folder, you should be able to change it to;

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

All 31 comments

The same here. What i have tried:

  1. .env in workspace root
    PYTHONPATH=./lib/python:${PYTHONPATH}

  2. .vscode/settings.json

{
    "terminal.integrated.env.linux": {
        "PYTHONPATH": "${workspaceFolder}/lib/python:${env:PYTHONPATH}"
    },
    "python.envFile": "${workspaceFolder}/.env",
    "python.analysis.extraPaths": ["${workspaceFolder}/lib/python"]
}

my workspace structure is:

| .vscode
| .env
| lib
- | python
--- | local
------ | __init__.py
------ | somePackage
---------| __init__.py
---------| ...

and during import
from local.somePackage import something
still have warning:
import could not be resolved

Thanks for the feedback.

The setting is supported.

I'm not sure what is happening in @mikelnrd 's case, but for @htarnacki it looks like the issue is that we don't support replacing variables such as $(workspaceFolder}. Since the path is relative to the workspace folder, you should be able to change it to;

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

The variables are something that the Python extension does, but isn't in the VS Code API or LSP. (microsoft/python-language-server#1783)

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

and this has resolved my problem. thx! ;-)

It doesn't work for me with ..
"python.analysis.extraPaths": ["../../lib/"]
I put it to wrong file (launch.json), it works fine after moving it to workspace settings.json

Hi folks, Pylance looks great. I just want to report the problem in my own project.
I have written several packages in my project, i.e., base, utils, dataset, where each package is built with __init__.py. But Pylance still couldn't resolve the imports, nor the functions or classes in there. Just a screenshot here:
image

Pylance needs to know which import resolution paths to use. By default, it uses the root of your workspace. You will need to add these additional paths using the "python.analysis.extraPaths" setting like this:
"python.analysis.extraPaths": ["base", "utils", "dataset", ""]
Note that these paths are relative to your workspace root directory.

This means that I need to include the extraPaths for each workspace I opened? If I do this, then will it have any impact on another different workspace? Thanks!

It depends on which settings.json you place the setting. If you want it to apply to all your workspaces, you can place it in the user settings. I would probably avoid doing this for python.analysis.extraPaths though, unless it really applies to all your Python projects on that machine.
https://code.visualstudio.com/docs/getstarted/settings

It doesn't look like python.analysis.extraPaths is documented on the README or the marketplace entry, so at the very least we should document it.

extraPaths is not working for me with Pylance.

My VSCode workspace is setup like this

<WorkspaceRoot>/tools/python_lib/mypkg/*.py

With the original language server the following works for me in the workspace settings.
I can navigate between files using F12
You will notice i dont prefix the paths with . If i do prefix, it does not work.

   "python.autoComplete.extraPaths": [
         "tools/python_lib",
      ],

With Pylance I have tried putting entries in the workspace settings and the folder settings. and nothing works.
This is from workspace settings
"python.autoComplete.extraPaths": [ "tools/python_lib/mypkg", "tools/python_lib", ],

And i tried this in folder settings

      "python.autoComplete.extraPaths": [
         "tools/python_lib/mypkg",
         "tools/python_lib",
      ],

Thanks

The name of the setting has changed to "python.analysis.extraPaths". We're trying to standardize on "python.analysis.x" for all type analysis features. Please give that a try and let us know if it works for you.

that resolved it. thank you.

Documentation was added to our README for python.analysis.extraPaths!

Finally, I was able to resolve the annoying import could not be resolved message. I have no idea why I have to take this extra step of specifying additional paths.

Anyway, for me, I decided to place "python.analysis.extraPaths": ["\\workspace_root\\allprojects\project1"] into the main settings.json for VS Code. Also, on Windows, remember to escape the backslashes \\ for the directory structure. Also, I didn't need to muck around by creating .env file within .vscode folder, and adding a bunch of time wasting code.

I have the same issue. When I use a visual python environment and "import tensorflow.keras", I get the error: Import "tensorflow.keras" could not be resolved. But the code can run.
ps: I have added "my env path/Scripts" and "my env path/Lib/site-packages" in the python.analysis.extraPaths.

You should never need to set extraPaths to resolve something installed in your environment. Please file a new issue (or see if #99 covers your problem).

You should never need to set extraPaths to resolve something installed in your environment. Please file a new issue (or see if #99 covers your problem).

Thanks

I have been trying to use Pylance with the latest public release of Code, and I am also experiencing problems using python.analysis.extraPaths.

I have a few folders of skeleton python files containing simple classes, methods, and functions without any of the arguments or anything provided (the skeletons just use args, *kwargs as the arguments). These are generated stand-ins for code completion as the libraries only ship as binaries with the application they come from. However, Pylance still reports that the modules cannot be found when trying to use code completion or looking at code that uses these libraries. I tried using complete paths, relatively pathing from the base of the workspace folder, and I could never get Pylance to pick up these directories and start using them for code completion or code checking.

I did however, find that taking all of these various skeletons and copying their modules and files into to a single directory, and pointing python.analysis.stubPath to that path, does get the auto-completion to kick in, and code analysis too. This might be a new issue or a regression, I'm just taking stabs in the dark and offering what I have found, if anyone else is still running into issues using Pylance. It's too bad stubPath only accepts a single string a not a list but at least I'm up and running for now!

I hope some of this is helpful, I'd be happy to spin up a separate issue with more details

Yes, that sounds like a separate issue as well. Typically the skeletons you've described are just type stubs; pyi files that describe the signatures of another library. The default place to put these is in the typings folder if they don't exist in the libraries themselves, but you're free to put your stubs wherever makes sense for your project and override the config. A stub path is the intended thing to use here (stubs only), as compared to extraPaths (which points to real code).

Apparently ${workspaceFolder} and ${workspaceFolderBasename} are not working with python.analysis.extraPaths

@maurokenny That's #548. Relative paths work in extraPaths.

I've read several tickets on this issue and haven't seen much advice on how python.analysis.extraPaths integrates with Conda. There's lots about how to link up a library that's in your workspace or project. But, when using Conda, I find that I end up with lots of Conda environments, none of which are in my current workspace. If you play around with ML projects on Github, almost every one of them specifies its own environment.

I'm using a multi-root workspace. So I thought that if I added an entry for python.analysis.extraPaths at the folder (per project) level, I'd be able to get rid of the Pylance import errors. You can do this in the VSCode GUI by typing Ctrl-Shift-P, then 'Open Workspace Settings.' Then type python.analysis.extraPaths, click the 'Folder' tab and enter your path.

But it's much easier to edit the vscode/settings.json file in your project to include:

{
    "python.analysis.extraPaths": [
        "/home/username/anaconda3/envs/conda-env-name/lib/python2.7/site-packages/" 
        /* Obviously, replace username and conda-env-name with real values. */
    ]
}

In my case I have to do this for each project in a multi-root workspace, as each has its own environment.

It's a shame that Pylance can't use variables like ${workspaceFolder} - hardcoding this path is kind of a drag. It's also a drag to have to do this for pretty much every project. But as of now, that's what seems to be required. Hopefully this will help someone out.

You should never need to set extraPaths to resolve an import coming from your interpreter or its installed packages. If imports are not resolving, that is a separate bug and we'd appreciate a separate issue of that. Note that we don't support (or plan to support) Python 2, if that affects things.

We respect the interpreter chosen by the Python extension; are you saying you need a different interpreter per workspace and the core extension doesn't do that? You may also be interested in a pyrightconfig with executionEnvironments: https://github.com/microsoft/pyright/blob/master/docs/configuration.md

"are you saying you need a different interpreter per workspace"

I often have to set different interpreters per project, not just workspace. If you look at the Conda environment.yml files of various ML projects, they specify different python versions because, usually, they implement different Tensorflow or Pytorch versions that require those versions of Python.

These projects are all in one multi-root workspace (seemed like a good idea - maybe not!). Until I set extraPaths in a vscode/settings.json file at the project level, Pylance would not resolve Tensorflow and Scipy imports.

I want to say that I've seen these problems with Python 3 projects, but now that I think about it, I'm not sure. I'll look into it and will post an update here. The fact that Pylance doesn't support Python 2 would at least explain the problem I'm seeing. If I only have to mess with vscode/settings when Python 2 is involved, that's not so bad.

I think that if you need that level of control and a wide range of environments even within the same workspace, you'd likely benefit from setting up execution environments.

image
pico8 import only works when the folder containing that file is opened, even with this in settings.json:

    "python.analysis.extraPaths": [
        "${fileDirname}",
        "."
    ]

Pylance doesn't support all of the different settings templates. See #548.

I have set

"python.analysis.extraPaths": [
        "C:\\Program Files\\Side Effects Software\\Houdini 18.5.445\\houdini\\python2.7libs"
    ]

in my settings.json (also tried with forward slashes), but it seems to be completely ignored. It's still claiming that module hou could not be resolved. Used to work with a different VSCode extension.

This issue is old and closed; it's unlikely your issue is related. You may want to open an new issue with trace logs so we can examine some of the import paths being used, though I will point out that we do not support Python 2.

Yea I found out it's because the thing only works when opening a folder but not on single files for some reason.

Was this page helpful?
0 / 5 - 0 ratings