insarlab / PySolid

A Python wrapper for solid Earth tides
GNU General Public License v3.0
62 stars 7 forks source link

Use numpy.f2py to build via setup.py/pip #19

Closed jhkennedy closed 2 years ago

jhkennedy commented 2 years ago

The ultimate goal of this PR is make windows builds via conda-forge (and therefore allow MintPy on windows via conda-forge insarlab/MintPy#648 ).

It uses the numpy.f2py package to provide compilation of the fortran sources at install time via setuptools/pip

 python -m pip install git+https://github.com/insarlab/PySolid.git 

which means:

Fixes #12 , #9

jhkennedy commented 2 years ago

I've opened a throw away PR to the PySolid feedstock showing this new build methodology works for Linux, Windows, and Mac conda-forge packaging: https://github.com/conda-forge/pysolid-feedstock/pull/3

yunjunz commented 2 years ago

This is great @jhkennedy!! I tried to follow your note to compile from the source using conda, and the installation seems to work fine.

There is one issue while running the test commands, as below:

(test) yunjunz:~/tools/PySolid>$ python -c "import pysolid; print(pysolid.__version__)"
Traceback (most recent call last):
  File "/Users/yunjunz/tools/PySolid/pysolid/grid.py", line 20, in <module>
    from .solid import solid_grid
ModuleNotFoundError: No module named 'pysolid.solid'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/yunjunz/tools/PySolid/pysolid/__init__.py", line 1, in <module>
    from .grid import *
  File "/Users/yunjunz/tools/PySolid/pysolid/grid.py", line 25, in <module>
    raise ImportError(msg)
ImportError: Cannot import name 'solid' from 'pysolid'!
    Maybe solid.for is NOT compiled yet.
    Check instruction at: https://github.com/insarlab/PySolid.

I believe this is because both grid.py and point.py use the local import of the pysolid.solid module. I am not sure if this is related to the libraries in setup.py. Any idea?

yunjunz commented 2 years ago

I am wondering did the-empty-solid.txt-file issue (https://github.com/insarlab/PySolid/issues/15) occurred to you for tests of pysolid.grid on Windows OS?

jhkennedy commented 2 years ago

@yunjunz huh, I can recreate the ImportError: Cannot import name 'solid' from 'pysolid'! when I install via pip without a -e, but solid is in the installed directory -- wonder why CircleCI or conda-forge isn't having the same problem? I'll see if I can figure out what's going on.

yunjunz commented 2 years ago

FYI, if I change the import inside grid.py and point.py from:

from .solid import solid_grid

to

from pysolid.solid import solid_grid

it works fine.

jhkennedy commented 2 years ago

@yunjunz yeah if I cd .. (out of the PySolid repo) it also works -- I think it's weird python shananigans picking up the local repository and not having the build .so inside it (like it does with an editable install).

I'll change the imports and poke at it a bit more

jhkennedy commented 2 years ago

@yunjunz I switch the relative imports to absolute, and fixed a little of the README instructions. Some people may still see the same import error if they pip install and then try to run/import pysolid from the the repository root, which is why Python tends to recomend a src layout (e.g., here and here). If you're working in the repository, an editable (-e) should always work fine and is better for development anyways.

yunjunz commented 2 years ago

Thank you @jhkennedy for the fix! The test passed on my macOS.

jhkennedy commented 2 years ago

@yunjunz I haven't hand a chance to look at #15 (tests work on linux, focused on the build working on windows), but can try that out as well (will just need to fire up my old desktop).