Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to override poetry python packages using JupyterWith UI? #413

Closed
michelav opened this issue Jan 10, 2023 · 2 comments
Closed

How to override poetry python packages using JupyterWith UI? #413

michelav opened this issue Jan 10, 2023 · 2 comments

Comments

@michelav
Copy link

michelav commented Jan 10, 2023

I need to extend kernel by using JupyterWith's configured poetry (can't build some libs when using extraPackages #395 ). I've tried to define my dependencies in pyproject.toml but it still fails for some libs (e.g. scikit-learn). There is a known issue related to that at poetry2nix. Meanwhile there is no better solution, I guess I'd have to override some libs (in my case packaging) and include fit-core as buildInput.

I'm trying to do that through an overrides file in the same dir my custom kernel is defined.

├── default.nix
├── overrides.nix
├── poetry.lock
└── pyproject.toml

This is the customized kernel:

{ name, availableKernels, extraArgs, ... }:
availableKernels.python {
  projectDir = ./.;
  displayName = "PD4ML Python";
  inherit (extraArgs) system;
  name = "pd4ml-python";
}

and the overrides file.

final: prev: {
  packaging = prev.packaging.overridePythonAttrs (old: {
    buildInputs = old.buildInputs or [ ] ++ [ final.python3.pkgs.flit-core ];
  });
}

This is my pyproject.toml.

[tool.poetry]
name = "pd4ml-python"
version = "0.1.0"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.9"
ipykernel = "*"
scikit-learn = "*"

[tool.poetry.dev-dependencies]
# build systems for dependencies
hatchling = "^1.3.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

After that I keep getting ModuleNotFoundError: No module named 'flit_core'.

Is this the right way to override packages in poetry through JupyterWith?

@michelav
Copy link
Author

OK. Got it working. To extend the kernel using poetry one must set up the projectDir so all setup files are read from there. However, the overrides attribute isn't set accordingly.

This was extracted from default python kernel. I guess overrides attribute should be read from projectDir as well.

{
 projectDir ? self + "/kernels/available/python",
  pyproject ? projectDir + "/pyproject.toml",
  poetrylock ? projectDir + "/poetry.lock",
  overrides ? poetry2nix.overrides.withDefaults (import ./overrides.nix),
}

In my case I had to add poetry2nix as input, set it up and plug it as extraArgs when configuring my JupyterLab.

Flake.nix

let
        python = pkgs.python3;
        poetry2nix =
          import "${inputs.poetry2nix}/default.nix" { inherit pkgs poetry; };

        poetry = pkgs.callPackage "${inputs.poetry2nix}/pkgs/poetry" {
          inherit python;
        };
        inherit (jupyterWith.lib.${system}) mkJupyterlabFromPath;
        jupyterlab =
          mkJupyterlabFromPath ./kernels { inherit system poetry2nix poetry; };
 in { ... }

kernels/custom-python/default.nix

{ name, availableKernels, extraArgs, ... }:
availableKernels.python rec {
  projectDir = ./.;
  displayName = "PD4ML Python";
  inherit (extraArgs) system poetry2nix;
  overrides = poetry2nix.overrides.withDefaults (final: prev: {
    packaging = prev.packaging.overridePythonAttrs
      (old: { buildInputs = old.buildInputs or [ ] ++ [ prev.flit-core ]; });
  });
  name = "pd4ml-python";
}

@djacu
Copy link
Contributor

djacu commented Jan 20, 2023

Glad you got it working!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants