From 98b0604eb90c2c1056161e660d3b082de5c66959 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sat, 13 May 2023 16:19:14 -0700 Subject: [PATCH] Added command-line option `pythonpath` that allows the path to the python interpreter to be specified. This addresses https://github.com/microsoft/pyright/issues/5111. --- docs/command-line.md | 15 +++++++++------ docs/settings.md | 2 +- packages/pyright-internal/src/pyright.ts | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/docs/command-line.md b/docs/command-line.md index 9855355e4..96f82d85f 100644 --- a/docs/command-line.md +++ b/docs/command-line.md @@ -13,25 +13,28 @@ Pyright can be run as either a VS Code extension or as a node-based command-line | --level | Minimum diagnostic level (error or warning) | | --outputjson | Output results in JSON format | | -p, --project `` | Use the configuration file at this location | +| --pythonpath `` | Path to the Python interpreter (2) | | --pythonplatform `` | Analyze for platform (Darwin, Linux, Windows) | | --pythonversion `` | Analyze for version (3.3, 3.4, etc.) | | --skipunannotated | Skip type analysis of unannotated functions | | --stats | Print detailed performance stats | -| -t, --typeshedpath `` | Use typeshed type stubs at this location (2) | -| -v, --venvpath `` | Directory that contains virtual environments (3) | +| -t, --typeshedpath `` | Use typeshed type stubs at this location (3) | +| -v, --venvpath `` | Directory that contains virtual environments (4) | | --verbose | Emit verbose diagnostics | | --verifytypes `` | Verify completeness of types in py.typed package | | --version | Print pyright version | | --warnings | Use exit code of 1 if warnings are reported | -| -w, --watch | Continue to run and watch for changes (4) | +| -w, --watch | Continue to run and watch for changes (5) | (1) If specific files are specified on the command line, it overrides the files or directories specified in the pyrightconfig.json or pyproject.toml file. -(2) Pyright has built-in typeshed type stubs for Python stdlib functionality. To use a different version of typeshed type stubs, specify the directory with this option. +(2) This option is the same as the language server setting `python.pythonPath`. It cannot be used with --venvpath. The --pythonpath options is recommended over --venvpath in most cases. For more details, refer to the [import resolution](import-resolution.md#configuring-your-python-environment) documentation. -(3) This option is used in conjunction with configuration file, which can refer to different virtual environments by name. For more details, refer to the [configuration](configuration.md) documentation. This allows a common config file to be checked in to the project and shared by everyone on the development team without making assumptions about the local paths to the venv directory on each developer’s computer. +(3) Pyright has built-in typeshed type stubs for Python stdlib functionality. To use a different version of typeshed type stubs, specify the directory with this option. -(4) When running in watch mode, pyright will reanalyze only those files that have been modified. These “deltas” are typically much faster than the initial analysis, which needs to analyze all files in the source tree. +(4) This option is the same as the language server setting `python.venvPath`. It used in conjunction with configuration file, which can refer to different virtual environments by name. For more details, refer to the [configuration](configuration.md) and [import resolution](import-resolution.md#configuring-your-python-environment) documentation. This allows a common config file to be checked in to the project and shared by everyone on the development team without making assumptions about the local paths to the venv directory on each developer’s computer. + +(5) When running in watch mode, pyright will reanalyze only those files that have been modified. These “deltas” are typically much faster than the initial analysis, which needs to analyze all files in the source tree. # Pyright Exit Codes diff --git a/docs/settings.md b/docs/settings.md index 3ea9bf877..8f0d0e804 100644 --- a/docs/settings.md +++ b/docs/settings.md @@ -32,5 +32,5 @@ The Pyright language server honors the following settings. **python.pythonPath** [path]: Path to Python interpreter. This setting is being deprecated by the VS Code Python extension in favor of a setting that is stored in the Python extension’s internal configuration store. Pyright supports both mechanisms but prefers the new one if both settings are present. -**python.venvPath** [path]: Path to folder with subdirectories that contain virtual environments. +**python.venvPath** [path]: Path to folder with subdirectories that contain virtual environments. The `python.pythonPath` setting is recommended over this mechanism for most users. For more details, refer to the [import resolution](import-resolution.md#configuring-your-python-environment) documentation. diff --git a/packages/pyright-internal/src/pyright.ts b/packages/pyright-internal/src/pyright.ts index 8b020a791..c8ebb35d5 100644 --- a/packages/pyright-internal/src/pyright.ts +++ b/packages/pyright-internal/src/pyright.ts @@ -136,6 +136,7 @@ async function processArgs(): Promise { { name: 'level', type: String }, { name: 'outputjson', type: Boolean }, { name: 'project', alias: 'p', type: String }, + { name: 'pythonpath', type: String }, { name: 'pythonplatform', type: String }, { name: 'pythonversion', type: String }, { name: 'skipunannotated', type: Boolean }, @@ -248,6 +249,18 @@ async function processArgs(): Promise { } } + if (args.pythonpath !== undefined) { + const incompatibleArgs = ['venv-path', 'venvpath']; + for (const arg of incompatibleArgs) { + if (args[arg] !== undefined) { + console.error(`'pythonpath' option cannot be used with '${arg}' option`); + return ExitStatus.ParameterError; + } + } + + options.pythonPath = combinePaths(process.cwd(), normalizePath(args['pythonpath'])); + } + if (args['venv-path']) { console.warn(`'venv-path' option is deprecated; use 'venvpath' instead`); options.venvPath = combinePaths(process.cwd(), normalizePath(args['venv-path'])); @@ -706,6 +719,7 @@ function printUsage() { ' --outputjson Output results in JSON format\n' + ' -p,--project Use the configuration file at this location\n' + ' --pythonplatform Analyze for a specific platform (Darwin, Linux, Windows)\n' + + ' --pythonpath Path to the Python interpreter\n' + ' --pythonversion Analyze for a specific version (3.3, 3.4, etc.)\n' + ' --skipunannotated Skip analysis of functions with no type annotations\n' + ' --stats Print detailed performance stats\n' +