3.5 KiB
Import Resolution
Resolution Order
If the import is relative (the module name starts with one or more dots), it resolves the import relative to the path of the importing source file.
For absolute (non-relative) paths, Pyright resolves imports in the following order:
-
Try to resolve using a stdlib typeshed stub. If the
typeshedPath
is configured, use this instead of the typeshed stubs that are packaged with Pyright. This allows for the use of a newer or a patched version of the typeshed stdlib stubs. -
Try to resolve using a local import. a. Try to resolve relative to the root directory of the execution environment. If no execution environments are specified in the config file, use the root of the workspace. For more information about execution environments, refer to the configuration documentation.
b. Try to resolve using any of the extra paths defined for the execution environment in the config file. If no execution environment applies, use the
python.analysis.extraPaths
setting. Extra paths are searched in the order in which they are provided in the config file or setting. -
Try to resolve using the stubPath as defined in the
stubPath
config entry or thepython.analysis.stubPath
setting. -
Try to resolve using a third-party typeshed stub. If the
typeshedPath
configured, use this instead of the typeshed stubs that are packaged with Pyright. This allows for the use of a newer or a patched version of the typeshed stdlib stubs. -
Try to resolve using the packages installed in the configured Python environment. For more details about how to configure your Python environment for Pyright, see below. If a Python environment is configured, Pyright looks in the
lib/site-packages
,Lib/site-packages
, orpython*/site-packages
subdirectory. If no site-packages directory can be found Pyright attempts to run the configured Python interpreter and ask it for its search paths. If no Python environment is configured, Pyright will use the default Python interpreter by invokingpython
.
Configuring Your Python Environment
Pyright does not require a Python environment to be configured if all imports can be resolved using local files and type stubs. If a Python environment is configured, it will attempt to use the packages installed in the site-packages
subdirectory during import resolution.
Pyright uses the following mechanisms (in priority order) to determine which Python environment to use:
-
If a
venv
name is specified for the execution environment along with apython.venvPath
setting (or a--venv-path
command-line argument), it appends the venv name to the specified venv path. -
If no
venv
name is specified for the execution environment but adefaultVenv
name is specified at the top level of the config file, use that venv name instead. -
If no
venv
ordefaultVenv
is specified in the config file, use thepython.pythonPath
setting. This setting is defined by the VS Code Python extension and can be configured using the Python extension’s environment picker interface. More recent versions of the Python extension no longer store the selected Python environment in thepython.pythonPath
setting and instead use a storage mechanism that is private to the extension. Pyright is able to access this through an API exposed by the Python extension. -
As a fallback, use the default Python environment (i.e. the one that is invoked when typing
python
in the shell).