问题
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:
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).
来源:https://stackoverflow.com/questions/60045913/installing-numpy-before-using-numpy-distutils-core-setup