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?

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
I was wrong; the error was caused by a completely different problem. Sorry
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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.