Is there a way to import anki python library somehow?

$ nix shell nixpkgs#anki nixpkgs#python3 --command python -c 'import anki'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'anki'

Looking at the source it looks like this is built as as an application instead of as a package, so that’s probably why, but I was wondering if there’s a workaround.

You need to use python3.withPackages (_: [ pkgs.anki ]) to set up your python with the module in its site-path. I’m not sure off the top of my head how you’d do that as a one-liner.

Recently talked about in the nix matrix

If you want to use nix3 (the new CLI) you can do something like nix shell --impure --expr 'with import <nixpkgs> {}; python3.withPackages (ps: [ anki ])'

If you’re fine with nix1, you could shorten that to nix-shell -p 'python3.withPackages (ps: [anki])' as the with pkgs; is implied there

That’ll usually work, but note that it is explicitly different from using nixpkgs#python3. You’re importing the nixpkgs from the NIX_PATH, not using your flake registry.

For the majority of people it will work anyway but yes, you’re right, if you want to be very sure you can do nix shell $(nix eval --raw nixpkgs#pkgs --apply 'pkgs: (pkgs.python3.withPackages (_: [ pkgs.anki ])).drvPath')

1 Like

At which point you’re better off just getting yourself a devshell

2 Likes