Skip to content

python312Packages.pymupdf: fix "mupdf not found" import error#334596

Closed
bennofs wants to merge 1 commit intoNixOS:masterfrom
bennofs:fix-pymupdf
Closed

python312Packages.pymupdf: fix "mupdf not found" import error#334596
bennofs wants to merge 1 commit intoNixOS:masterfrom
bennofs:fix-pymupdf

Conversation

@bennofs
Copy link
Contributor

@bennofs bennofs commented Aug 14, 2024

Description of changes

Fixes the following error:

$ nix shell --impure -I nixpkgs=flake:github:NixOS/nixpkgs/nixos-24.05 --expr 'with import <nixpkgs> {}; python3.withPackages (p: [p.pymupdf])' -c python3 -c 'import fitz'
Traceback (most recent call last):
  File "/nix/store/73znvwd538dglfsk2wdixj106i5infjn-python3-3.11.9-env/lib/python3.11/site-packages/fitz/__init__.py", line 140, in <module>
    from . import mupdf
ImportError: cannot import name 'mupdf' from partially initialized module 'fitz' (most likely due to a circular import) (/nix/store/73znvwd538dglfsk2wdixj106i5infjn-python3-3.11.9-env/lib/python3.11/site-packages/fitz/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/nix/store/73znvwd538dglfsk2wdixj106i5infjn-python3-3.11.9-env/lib/python3.11/site-packages/fitz/__init__.py", line 142, in <module>
    import mupdf

Things done

  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandboxing enabled in nix.conf? (See Nix manual)
    • sandbox = relaxed
    • sandbox = true
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 24.11 Release Notes (or backporting 23.11 and 24.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
  • Fits CONTRIBUTING.md.

Add a 👍 reaction to pull requests you find important.

@github-actions github-actions bot added the 6.topic: python Python is a high-level, general-purpose programming language. label Aug 14, 2024
@bennofs bennofs requested a review from teto August 14, 2024 11:02
@ofborg ofborg bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux. labels Aug 14, 2024
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 4a3db5a (#298783) @dotlambda proposed to move it to buildInputs. What is more correct? I am undecided.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are buildInputs even included in the environment when using pythonWithPackages envs? Mupdf itself provides a python module, so it needs to be part of the env, and I don't see how moving this to buildInputs changes that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I am not 100% familiar with the python nix packaging. From the documentation, I think the most correct would be to put it in "dependencies"?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, dependencies would be best. However it is currently roughly equivalent to propagatedBuildInputs anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dependencies sounds correct to me.

@dotlambda dotlambda changed the title pymupdf: fix "mupdf not found" import error python312Packages.pymupdf: fix "mupdf not found" import error Aug 15, 2024
Copy link
Member

@dotlambda dotlambda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the commit message.

@bennofs
Copy link
Contributor Author

bennofs commented Sep 3, 2024

Should we merge this?

@l0b0
Copy link
Contributor

l0b0 commented Sep 13, 2024

How do I test this? I tried to set

nixpkgs.overlays =
  let
    owner = "bennofs";
    branchname = "fix-pymupdf";
    pkgsReview = pkgs.fetchzip {
      url = "https://github.com/${owner}/nixpkgs/archive/${branchname}.tar.gz";
      sha256 = "sha256-i6rDWzvPHFl143VzJmBzhRzlz10SKewe4uuRarPUygY=";
    };
  in
  [
    (self: super: {
      review = import pkgsReview {
        overlays = [ ];
        inherit (super) config;
      };
      python312Packages.pymupdf = self.review.python312Packages.pymupdf;
    })
  ];

but that results in errors "attribute 'pip' missing" and "attribute 'python' missing".

@elohmeier elohmeier mentioned this pull request Nov 1, 2024
13 tasks
@elohmeier
Copy link
Contributor

This can be tested using nix-build -E 'with import ./. {}; python3.withPackages (ps: [ ps.pymupdf ])' and then ./result/bin/python3 -c 'import pymupdf; print(pymupdf.__version__)'.

Before the PR:

$ nix-build -E 'with import ./. {}; python3.withPackages (ps: [ ps.pymupdf ])'
/nix/store/37mb5xk23v4jrzy46acpval4wwhiiw91-python3-3.12.7-env
$ ./result/bin/python3 -c 'import pymupdf; print(pymupdf.__version__)'
Traceback (most recent call last):
  File "/nix/store/37mb5xk23v4jrzy46acpval4wwhiiw91-python3-3.12.7-env/lib/python3.12/site-packages/pymupdf/__init__.py", line 196, in <module>
    from . import mupdf
ImportError: cannot import name 'mupdf' from partially initialized module 'pymupdf' (most likely due to a circular import) (/nix/store/37mb5xk23v4jrzy46acpval4wwhiiw91-python3-3.12.7-env/lib/python3.12/site-packages/pymupdf/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/nix/store/37mb5xk23v4jrzy46acpval4wwhiiw91-python3-3.12.7-env/lib/python3.12/site-packages/fitz/__init__.py", line 2, in <module>
    from pymupdf import *
  File "/nix/store/37mb5xk23v4jrzy46acpval4wwhiiw91-python3-3.12.7-env/lib/python3.12/site-packages/pymupdf/__init__.py", line 198, in <module>
    import mupdf
ModuleNotFoundError: No module named 'mupdf'

After the PR:

$ nix-build -E 'with import ./. {}; python3.withPackages (ps: [ ps.pymupdf ])'
/nix/store/k6xajrhjnyp9g6fglxxazqw62awm240j-python3-3.12.7-env
$ ./result/bin/python3 -c 'import pymupdf; print(pymupdf.__version__)'
1.24.10

This PR actually fixes the import error, since withPackages now picks up the Python module from mupdf-cxx, but I would still like to see another approach in the pymupdf module which just copies the mupdf module into the output of the pymupdf derivation. This also ensures compatibility with pyproject.nix / uv2nix (since that uses its own approach for dependency resolution) and would be more in line with how the upstream PyPI package works. See this PR for that solution: #352845

I've tested this on x86_64-linux, aarch64-linux and aarch64-darwin.

(If you want to test this on Darwin make sure to apply the related tesseract-codesigning-fix from here first: #352788)

@dotlambda
Copy link
Member

ModuleNotFoundError: No module named 'mupdf'

Any idea why that's not caught by pythonImportsCheck?

@wegank wegank added the 2.status: merge conflict This PR has merge conflicts with the target branch label Jun 9, 2025
@Samuel-Martineau
Copy link
Contributor

Out of curiosity, is there anything blocking this from moving forward?

@phanirithvij
Copy link
Member

phanirithvij commented Jun 13, 2025

there is a merge conflict @bennofs

Edit: I just noticed you are in Nixpkgs/retired-nixpkgs-contributors, can you clarify here once if you intend to see this pr through or whether someone else can pick it up.

@bennofs
Copy link
Contributor Author

bennofs commented Jul 19, 2025

there is a merge conflict @bennofs

Edit: I just noticed you are in Nixpkgs/retired-nixpkgs-contributors, can you clarify here once if you intend to see this pr through or whether someone else can pick it up.

I'm happy if someone else picks it up, no need to wait for me :)

@bennofs
Copy link
Contributor Author

bennofs commented Jul 19, 2025

This PR actually fixes the import error, since withPackages now picks up the Python module from mupdf-cxx, but I would still like to see another approach in the pymupdf module which just copies the mupdf module into the output of the pymupdf derivation. This also ensures compatibility with pyproject.nix / uv2nix (since that uses its own approach for dependency resolution) and would be more in line with how the upstream PyPI package works. See this PR for that solution: #352845

Wouldn't uv2nix then have the same problem for any other python package that use the nixpkgs way to depend on native packages? Or is there something special about pymupdf here.

My opinion is that we should be consistent within nixpkgs, and I believe the rest of nixpkgs uses the toPythonModule way of depending on python modules provided by native packages.

Fixes the following error:

```
Traceback (most recent call last):
  File "/nix/store/73znvwd538dglfsk2wdixj106i5infjn-python3-3.11.9-env/lib/python3.11/site-packages/fitz/__init__.py", line 140, in <module>
    from . import mupdf
ImportError: cannot import name 'mupdf' from partially initialized module 'fitz' (most likely due to a circular import) (/nix/store/73znvwd538dglfsk2wdixj106i5infjn-python3-3.11.9-env/lib/python3.11/site-packages/fitz/__init__.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/nix/store/73znvwd538dglfsk2wdixj106i5infjn-python3-3.11.9-env/lib/python3.11/site-packages/fitz/__init__.py", line 142, in <module>
    import mupdf
```
@bennofs
Copy link
Contributor Author

bennofs commented Jul 19, 2025

Actually, this change has already been applied on master independently of this PR, as commit 36354c7. So this PR is no longer relevant.

@bennofs bennofs closed this Jul 19, 2025
@nixpkgs-ci nixpkgs-ci bot removed the 2.status: merge conflict This PR has merge conflicts with the target branch label Jul 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

6.topic: python Python is a high-level, general-purpose programming language. 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 0 This PR does not cause any packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.