Repository navigation
pyodide-build: unable to locate numpy package while building the pymatgen package聽#4347
Description
馃悰 Bug
During the build of a pymatgen wasm/emscripten whl file following the tutorial: https://pyodide.org/en/stable/development/building-and-testing-packages.html, the 'pyodide build' command returns an error: "ModuleNotFoundError: No module named 'numpy'
To Reproduce
git clone https://github.com/materialsproject/pymatgen
then follow this tutorial
Expected behavior
build pymatgen-cp310-cp310-emscripten_3_1_27_wasm32.whl file
Environment
- Pyodide Version: 0.2.3
- Browser version: 120.0.6099.71
- Any other relevant information: Pymatgen version: 2023.11.18
Error Log
xbuild environment already exists, skipping download
Installing xbuild environment
* Creating virtualenv isolated environment...
* Installing packages in isolated environment... (Cython>=0.29.23, setuptools>=65.0.0, versioneer[toml], wheel)
* Getting dependencies for wheel...
Traceback (most recent call last):
File "/home/jvanbuskirk/.local/lib/python3.10/site-packages/pep517/in_process/_in_process.py", line 351, in <module>
main()
File "/home/jvanbuskirk/.local/lib/python3.10/site-packages/pep517/in_process/_in_process.py", line 333, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/home/jvanbuskirk/.local/lib/python3.10/site-packages/pep517/in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
File "/tmp/build-env-bmttes29/lib/python3.10/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
File "/tmp/build-env-bmttes29/lib/python3.10/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
self.run_setup()
File "/tmp/build-env-bmttes29/lib/python3.10/site-packages/setuptools/build_meta.py", line 311, in run_setup
exec(code, locals())
File "<string>", line 8, in <module>
ModuleNotFoundError: No module named 'numpy'
* Getting dependencies for wheel...
Traceback (most recent call last):
File "/home/jvanbuskirk/.local/lib/python3.10/site-packages/pep517/in_process/_in_process.py", line 351, in <module>
main()
File "/home/jvanbuskirk/.local/lib/python3.10/site-packages/pep517/in_process/_in_process.py", line 333, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/home/jvanbuskirk/.local/lib/python3.10/site-packages/pep517/in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
File "/tmp/build-env-bmttes29/lib/python3.10/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
File "/tmp/build-env-bmttes29/lib/python3.10/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
self.run_setup()
File "/tmp/build-env-bmttes29/lib/python3.10/site-packages/setuptools/build_meta.py", line 311, in run_setup
exec(code, locals())
File "<string>", line 8, in <module>
ModuleNotFoundError: No module named 'numpy'
ERROR Backend subproccess exited when trying to invoke get_requires_for_build_wheel
Additional context
As stated in the tutorial, I verified that I can build the whl file with the regular
pip install build
python -m build
workflow, and indeed, that resulted in a whl file appropriate for my local operating system (Ubuntu 22.04.3, x86_64)
This issue seems to be related to an existing issue: #4160 but pymatgen comes with a pyproject.toml, which requires setuptools, not meson for building. In the other reported issue, pandas appeared to give a similar error when compilation occured with setuptools, but in pandas 2.1.0, the pyproject.toml uses meson, and deos not give the error. Is it possible the error is related to the use of setuptools?
Activity
stefdoerr commentedon Dec 20, 2023
I am getting the same error when building moleculekit
If you look at the
pyproject.tomlit includes numpy in the build dependenciesbut when running
pyodide-buildit removes it from the packagesI played around to see if it was maybe reading these packages from elsewhere and removed versioneer from the list and indeed
pyodide-buildupdated that message, so it's reading them frompyproject.tomlbut for some reason it ignoresnumpywhen it's in that list.python -m buildworks perfectly finestefdoerr commentedon Dec 20, 2023
I dug into the source code.
In the file
pypabuild.pywhich is the modified pypa/build used by pyodide-build, there is a functionremove_avoided_requirementswhich takes as inputget_unisolated_packages() + AVOIDED_REQUIREMENTS.Looking at
build_env.pyin theget_unisolated_packages()it reads a file/home/user/moleculekit/.pyodide-xbuildenv/xbuildenv/pyodide-root/unisolated.txtwhich containsnumpyin it. So indeed pyodide-build is removing on purpose numpy from our build requirements. I don't understand why though.stefdoerr commentedon Dec 20, 2023
Removing
numpyfrom theunisolated.txtfile continues along with the build but eventually crashes with:Which according to this stackoverflow question from 2 years ago is a bug in python 3.10 and numpy? https://stackoverflow.com/a/66908804
I'm out of ideas
jonvanbuskirk0 commentedon Dec 21, 2023
Hi, Stefan, thanks for sharing your similar error. Hopefully this is addressed soon. As an update, I did manage to make a little more progress.
Since you were able to determine the error is confined to python3.10 and numpy, I went ahead and installed a local copy of python3.11, and all of the requisite packages, hoping to simply repeat the process with the newer python version.
Unfortunately, pymatgen does not have a
/home/user/pymatgen/.pyodide-xbuildenv/directory, but I was eventually able to get it running on pyodide anyways by hosting a local copy of the .tar.gz file from https://pypi.org/project/pymatgen/#files following this procedure: https://pyodide.org/en/stable/usage/loading-custom-python-code.html#from-python. Note, if you do this, you'll need to extract the [package] directory from the untared [package]-X.X.X directory, then retar that into the .tar.gz you use to upload.This worked for pymatgen, but then I had to go through and find/install all of it's dependencies manually. This led me to needing to repeat the process for another unsupported package, spglib. This package has a
.pyodide-buildenv/directory though. Following your procedure of removing numpy from the/home/user/package/.pyodide-xbuildenv/xbuildenv/pyodide-root/unisolated.txtfile, I was able to callpyodide buildin the/home/user/packagedirectory, and build a -none-any.whl file.Unfortunately, now this wheel file is being installed correctly, but is not being seen by pymatgen. I can call
micropip.install("./spglib-2.2.0-py3-none-any.whl");, and importing micropip and callingmicropip.list()shows spglib is imported, but I still getModuleNotFoundError: No module named 'spglib'when one of the pymatgen modules callsimport spglib.Does anyone have any thoughts about this?
stefdoerr commentedon Apr 26, 2024
Any update on this issue? It's still around in the latest pyodide-build version 0.25.1
jonvanbuskirk0 commentedon Apr 30, 2024
Hi Stefan!
Sort of. It's been a while, so this may be slightly inaccurate, but I never got pymatgen to build with the necessary numpy dependency. Instead, I noticed that I didn't intend to use the portion of pymatgen that required numpy, so I edited my local copy of pymatgen to remove the parts that required numpy, essentially removing functionality from pymatgen. While this is a terrible practice for a plethora of reasons, it did work...
Best of luck!
hoodmane commentedon Apr 30, 2024
I guess we should look into this @ryanking13...
ryanking13 commentedon May 1, 2024
Oh, thanks for the ping @hoodmane. I must have missed this issue.
This is intended. WebAssembly is a 32-bit environment, so if you install numpy from PyPI when you build, some headers will be incorrect because their values are set based on a 64-bit system. So instead of pulling numpy from PyPI, we redirect it to the numpy we built for WebAssembly to handle it, but I think something was going wrong and this wasn't working properly.
stefdoerr commentedon May 6, 2024
@ryanking13 I see, but the issue is that my package contains some cython modules and that cython code uses numpy types so we need to have numpy installed to be able to compile the cython code. This used to work in the past as I was able to correctly build and use my packages through pyodide including the cython code.
hoodmane commentedon May 6, 2024
@stefdoerr Are you building with Python 3.11?
hoodmane commentedon May 6, 2024
I just successfully built moleculekit with the following:
so it is at least possible to build pymatgen. It would help if you could give more precise information about what you did for us to reproduce it.
hoodmane commentedon May 6, 2024
Ah from your message above:
it sounds like you are trying to build with Python3.10 but you need to use Python 3.11. I think that'll fix your trouble. We should have better error reporting for this...
stefdoerr commentedon May 7, 2024
Oh well.. that fixed it 馃槃 Thanks for the help! But yes that error is really not very helpful, I thought it meant that it had trouble with both python 3.10 and 3.11 not that I should switch from 3.10 to 3.11.