Anybody know a way to get Pylance to recognize Python modules included via `python3.withPackages`?

I’m trying to develop something using PyQt6, and I’ve run into a bit of an issue with VSCode. I’ve tried both using purely Nix and using a venv, but Nix makes the editor integration fail and venv makes the program fail to launch due to a missing

I have the following shell.nix:

{ pkgs ? import <nixpkgs> { } }:

let myPythonPkgs = pypkgs: with pypkgs; [

in pkgs.mkShell {
  packages = with pkgs; [
    (python3.withPackages myPythonPkgs)

I’m using the direnv extension to make this available in VSCode.

This causes a weird issue: Pylance picks up the modules but fails to pick up the identifiers exported by these modules. It shows no error on the imports but shows an error on every use of an identifier from these modules.

Does anybody know how I can get Pylance to recognize the modules introduced via Nix properly?

1 Like

Just a guess, but does it work if you use python3.withPackages (...).env as the shell itself (for now)? That should properly set PYTHONPATH if your current shell doesn’t, and that should let Pylance pick up the modules.

I could be totally wrong though, sorry.

Yes, that worked! Unfortunately now I don’t have the ability to add any additional external programs to the environment. Is there some way I can do that?

Interestingly, it only works with some packages. For instance, pyqt6 works & is recognized by Pylance, but moviepy isn’t.

What’s the value of PYTHONPATH?

$ echo -- $PYTHONPATH

It’s blank.

(I also tested using Bash)

This works in poetry2nix.mkPoetryEnv, it might work in python3.withPackages too.

I was able to get it to recognize pwntools with python3.withPackages in users.user.<user>.packages
by adding the (sadly absolute) path of where it is installed in nix-store to settings.json as "python.analysis.extraPaths": [ "/nix/store/lkb8qcvlzaib80brshzf7pksgnd1xdnm-python3.10-pwntools-4.11.0/lib/python3.10/site-packages" ],

aquired through doing a pwd -P in /etc/profiles/per-user/<user>/lib/python3.10/site-packages/pwn

appearently this also works better after Import resolution not respecting virtual environments · Issue #5943 · microsoft/pyright · GitHub got merged, beginning in Pylance v2023.9.21