Mirrored from this comment
The advice by @uglycoyote above works, with some caveats. Not that you actually have to run .\nodeenv\scripts\Activate.ps1
instead of \nodeenv\Scripts\activate
in his version.
But let's say you have a virtual environment for your project at .venv
in the root of the project, with project dependencies (say pandas
) in there, as well as pyright
installed via pip install pyright
. If you had run npx -y pyright
out of a separate nodeenv
folder like the above solution, it won't "know" about your pandas
dependency over in .venv
, and will complain whenever it sees pandas
in your code.
But it turns out you can instruct nodeenv
to fix up your current .venv
as follows, which allows pyright
obtained from PyPi
to run in a dependency-aware way without the "return non-zero exit status 2" error. This is how you do that:
. '.venv/Scripts/activate'
pip install nodeenv
nodeenv -p
Note that pip install nodeenv
may be unnecessary, since pyright
has nodeenv
as a dependency. But let's be explicit.
If your .venv
already has pyright
installed via pip install pyright
or similar, then running pyright
at the terminal should work now. Running nodeenv -p
modified our .venv/Scripts/activate
script, exposing npm
and such to the command line. But we are actually just taking advantage of a side-effect that now pyright
should work without any additional npm install
or npx
commands.
Of course you could omit pip install pyright
, and instead source it via npm install -g pyright
now, which will source Pyright directly from NPM. But maybe you have a requirements.txt
somewhere with pyright==X.X.XXX
pinned to a version in it (like I do), and you'd like to avoid lugging around a package.json
just for a single NPM dependency in your pure Python project. Either way, by manually handling nodeenv
ourselves, we sidestep this issue and have pyright
run in a dependency-aware fashion.
Remember you have to run . '.venv/Scripts/activate
whenever you start your terminal. It's usually a good idea to do this in your terminal profile, like ~\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
for PowerShell on Windows, the equivalent of a .bashrc
.
You could also just install NodeJS on your Windows machine, side-stepping nodeenv
and pip install pyright
altogether. You would source pyright
directly from your system global NodeJS via npm install -g pyright
. If you want to install a specific version of pyright on a per-project basis, you would have to keep a package.json
in your repo that looks like this:
{
"private": true,
"devDependencies": {
"pyright": "X.X.XXX"
}
}
You would then run something like npm install
or npm install --no-save
(to avoid writing the lockfile). You would have to make sure the node_modules
or node_modules/.bin
directory was added to your system path to get access to pyright
on command line. If you do your order of operations correctly (activating .venv
as well), you can run pyright
in a dependency-aware manner like this.
Here's a more verbose example where I set up the environment and give comments along the way as to the specifics of this example. Your approach will likely vary depending on your tooling:
# Install the virtual environment
# For example, install using the default `py` launcher for Windows, optionally specifying the Python version
py -3.10 -m venv .venv
# Activate the virtual environment, exposing isolated scripts like `pip`, `python`, etc.
. '.venv/Scripts/activate'
# It's usually a good idea to upgrade pip and install the basics like this.
# You might also install your package manager here, like `flit` or `poetry`.
python -m pip install -U pip # Need to do it via `python -m ...` on Windows for reasons
pip install -U setuptools wheel # Can just use `pip` directly going forward
# Install the local package in editable mode, along with its dependencies.
# This will differ depending on your package manager of choice.
pip install -e .
# Let's say these are a mix of your package requirements and development requirements.
# In practice these would probably be specified in requirements files, `setup.py`, or `pyproject.toml`.
# Note that we are installing the Python wrapper for pyright here.
pip install pandas nodeenv pyright
# We need to manually run `nodeenv` here to avoid the "return non-zero exit status 2" issue
# The `-p` flag causes node to install to your current `.venv`, modifying '.venv/Scripts/activate' accordingly.
nodeenv -p
# Let's try pyright and see if it works!
pyright