如何从 CLI 安装和 运行 Pyright 而不是使用 VS 代码?

How do I install and run Pyright from the CLI instead of using VS Code?

我想开始使用 Pyright with my team's projects. I have installed the Visual Studio Code plugin,并且在工作时可以在我的编辑器中看到类型错误。但我希望能够 运行 它自动作为我们测试套件的一部分,包括在我们的 CI 服务器上,而 VS 代码插件无法做到这一点。我需要安装它并将其用作独立的命令行工具。

Pyright 的文档说它有一个命令行界面,但唯一的安装和构建说明是针对 Visual Studio 代码扩展的,安装它并没有添加 pyright 可执行文件:

$ pyright
-bash: pyright: command not found

如何从命令行在我的 Python 项目上安装和 运行 Pyright?

虽然它是 Python 程序员的工具,但 Pyright 是在 TypeScript 运行ning on Node 和 the documentation tells us that it can be installed using one of the package managers for that ecosystem, such as NPM or Yarn 中实现的。如果你想安装它以便在系统的任何地方使用(而不仅仅是在特定的 Node 项目中),你需要 global 安装。

npm install --global pyright

yarn global add pyright

这将添加 pyright 命令行界面。

$ pyright
Usage: pyright [options] file...

您可以 运行 通过在命令行中指定文件来针对您的文件进行此操作,但对于一个项目,您可能需要 create a pyrightconfig.json file identifying the files to be type-checked, and the Python interpreters, libraries, and type definitions that it should use. Some of these options can also be passed as command-line arguments,但不是全部。