4

I am using numpy.distutils to setup a package (mypackage) that has a frotran module. The problem is that if I do pip install mypackage on an environment that does not have numpy, I get the following error:

ModuleNotFoundError: No module named 'numpy'

The easy solution is to ask users (if I manage to have any) to pip install numpy before they install my package, but I do not think this is a very elegant solution.

I came up with the idea of calling setuptools.setup with only setup_requires=['numpy'] before I import numpy and it seems to work well. This is my setup.py:

import setuptools

setuptools.setup(
    setup_requires=[
        'numpy'
    ],)

from numpy.distutils.core import setup, Extension

mod = Extension(name='mypackage.amodule', sources=['source/a.f90'])

setup(name='mypackage',
      packages=['mypackage'],
      ext_modules=[mod],)

I honestly don't fully understand what it implies to call an empty setup() (no name, no package). Is this a good solution? Is this somehow a bad practice?

Felix
  • 170
  • 9

1 Answers1

2

It is a common issue. How to install a build-time dependency? You might want to use a pyproject.toml file and take advantage of the build-system feature. See PEP517. And an example here:

[build-system]
build-backend = "setuptools.build_meta"
requires = ["setuptools", "numpy"]

Use the pep517 tool to build the distributions (sdist and wheel).

sinoroc
  • 18,409
  • 2
  • 39
  • 70
  • I have tried this but it does not seem to build the module (amodule). Could it be that I have to use a different backend? – Felix Feb 15 '20 at 07:44
  • @Felix Maybe add an update to your question, to show your progress. In your `setup.py` make sure to remove the first `setuptools.setup(...)` call. The rest seems fine. – sinoroc Feb 15 '20 at 17:19
  • I was wrong; the error was caused by a completely different problem. Sorry – Felix Feb 16 '20 at 23:03
  • 1
    It seems that this will not install numpy. The build works correctly, but `python -c "import numpy"` returns an error. **So, if you need numpy for your package, you should still have it listed in `install_requires` of the `setup.py`**. Is that correct? – Felix Feb 16 '20 at 23:13
  • 1
    @Felix Yes, _build-time_ and _install-time_ dependencies can be two completely different lists. So eventually some dependencies might need to be listed in both, like _numpy_ in your case apparently. – sinoroc Feb 17 '20 at 08:47