Pylance not detecting python in visual studio code

Hi everyone,

I’m currently facing an issue with my NixOS setup for a Django development environment, and I’m hoping someone here might have some insight. Here is what I have set up:

I’m using the following flake configuration:

{
  description = "Document Manager API Server";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
    flake-utils.url = "github:numtide/flake-utils";
    flake-utils.inputs.nixpkgs.follows = "nixpkgs";
    devenv.url = "github:cachix/devenv";
    devenv.inputs.nixpkgs.follows = "nixpkgs";
    flake-parts.url = "github:hercules-ci/flake-parts";
    flake-parts.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = inputs@{ flake-parts, nixpkgs, ... }:
    flake-parts.lib.mkFlake { inherit inputs; } {
      imports = [ inputs.devenv.flakeModule ];
      systems = nixpkgs.lib.systems.flakeExposed;
      perSystem = { config, pkgs, ... }: {
        devenv.shells.default = {
          dotenv.disableHint = true;
          languages.python = {
            enable = true;
            uv.enable = true;
            uv.sync.enable = true;
          };
          processes.django.exec = "uv run python manage.py runserver";
          packages = [
            pkgs.just
            pkgs.poppler_utils
            pkgs.nodePackages.localtunnel
            pkgs.python310Packages.ipython
          ];
        };
      };
    };
}

This setup works great with Neovim; LSP functionality is seamless. However, I’m running into problems with VSCode, specifically with the Pylance extension. Even when I manually set the Python environment in VSCode, I get the following logs:

2024-11-14 09:20:35.403 [info] [Info  - 9:20:35 AM] (81621) Loading pyproject.toml file at /home/filip/dev/api-server/pyproject.toml
2024-11-14 09:20:35.404 [info] [Info  - 9:20:35 AM] (81621) Setting environmentName for service "api-server": "3.12.5 (global)"
2024-11-14 09:20:35.404 [info] [Info  - 9:20:35 AM] (81621) Setting pythonPath for service "api-server": "/nix/store/h3i0acpmr8mrjx07519xxmidv8mpax4y-python3-3.12.5/bin/python"
2024-11-14 09:20:35.405 [info] [Info  - 9:20:35 AM] (81621) No include entries specified; assuming /home/filip/dev/api-server
2024-11-14 09:20:35.405 [info] [Info  - 9:20:35 AM] (81621) Auto-excluding **/node_modules
2024-11-14 09:20:35.406 [info] [Info  - 9:20:35 AM] (81621) Auto-excluding **/__pycache__
2024-11-14 09:20:35.406 [info] [Info  - 9:20:35 AM] (81621) Auto-excluding **/.*
2024-11-14 09:20:35.433 [info] [Info  - 9:20:35 AM] (81621) Assuming Python version 3.12.5.final.0
2024-11-14 09:20:35.498 [info] [Info  - 9:20:35 AM] (81621) Found 38 source files
2024-11-14 09:20:35.541 [info] [Info  - 9:20:35 AM] (81621) Setting environmentName for service "<default>": "3.12.5 (global)"
2024-11-14 09:20:35.542 [info] [Info  - 9:20:35 AM] (81621) Setting pythonPath for service "<default>": "/nix/store/h3i0acpmr8mrjx07519xxmidv8mpax4y-python3-3.12.5/bin/python"
2024-11-14 09:20:35.542 [info] [Info  - 9:20:35 AM] (81621) No include entries specified; assuming /<default workspace root>
2024-11-14 09:20:35.542 [info] [Info  - 9:20:35 AM] (81621) Auto-excluding **/node_modules
2024-11-14 09:20:35.543 [info] [Info  - 9:20:35 AM] (81621) Auto-excluding **/__pycache__
2024-11-14 09:20:35.543 [info] [Info  - 9:20:35 AM] (81621) Auto-excluding **/.*
2024-11-14 09:20:35.583 [info] [Info  - 9:20:35 AM] (81621) Assuming Python version 3.12.5.final.0
2024-11-14 09:20:35.659 [info] [Error - 9:20:35 AM] (81621) File or directory "/<default workspace root>" does not exist.
2024-11-14 09:20:35.659 [info] [Info  - 9:20:35 AM] (81621) No source files found.
2024-11-14 09:20:36.154 [info] [Info  - 9:20:36 AM] (81621) Indexer background runner(6) root directory: file:///home/filip/.vscode/extensions/ms-python.vscode-pylance-2024.11.2/dist (index)
2024-11-14 09:20:36.154 [info] [Info  - 9:20:36 AM] (81621) Indexing(6) started
2024-11-14 09:20:36.554 [info] [Info  - 9:20:36 AM] (81621) scanned(6) 29 files over 1 exec env
2024-11-14 09:20:36.703 [info] [Info  - 9:20:36 AM] (81621) indexed(6) 29 files over 1 exec env
2024-11-14 09:20:36.718 [info] [Info  - 9:20:36 AM] (81621) Indexing finished(6).

I also use direnv to automatically load the environment, and my .envrc file looks like this:

echo "Loading environment..."
watch_file flake.nix
use flake . --impure

Despite setting the Python path manually, Pylance in VSCode seems to fail to pick up the correct environment or path, resulting in errors about missing files or directories and not finding source files.

Has anyone encountered similar issues or have suggestions on how to better configure Pylance to work with this NixOS-based development environment? Any tips or alternative approaches would be greatly appreciated!

Thanks in advance for your help!

1 Like