I have a project which has a C extension which requires numpy. Ideally, I'd like whoever downloads my project to just be able to run python setup.py install
or use one call to pip
. The problem I have is that in my setup.py
I need to import numpy to get the location of the headers, but I'd like numpy to be just a regular requirement in install_requires
so that it will automatically be downloaded from the Python Package Index.
Here is a sample of what I'm trying to do:
from setuptools import setup, Extension import numpy as np ext_modules = [Extension('vme', ['vme.c'], extra_link_args=['-lvme'], include_dirs=[np.get_include()])] setup(name='vme', version='0.1', description='Module for communicating over VME with CAEN digitizers.', ext_modules=ext_modules, install_requires=['numpy','pyzmq', 'Sphinx'])
Obviously, I can't import numpy
at the top before it's installed. I've seen a setup_requires
argument passed to setup()
but can't find any documentation on what it is for.
Is this possible?
Installing Python Packages with Setup.py To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.
The only prerequisite for installing NumPy is Python itself. If you don't have Python yet and want the simplest way to get started, we recommend you use the Anaconda Distribution - it includes Python, NumPy, and many other commonly used packages for scientific computing and data science.
Once you have the latest Python installed, you can proceed to install NumPy using Pip on Windows 10. Now, If you are using an older version of Python on Windows, you may need to install PIP manually. Pip is automatically installed with Python 2.7.
The following works at least with numpy1.8 and python{2.6,2.7,3.3}:
from setuptools import setup from setuptools.command.build_ext import build_ext as _build_ext class build_ext(_build_ext): def finalize_options(self): _build_ext.finalize_options(self) # Prevent numpy from thinking it is still in its setup process: __builtins__.__NUMPY_SETUP__ = False import numpy self.include_dirs.append(numpy.get_include()) setup( ... cmdclass={'build_ext':build_ext}, setup_requires=['numpy'], ... )
For a small explanation, see why it fails without the "hack", see this answer.
Note, that using setup_requires
has a subtle downside: numpy will not only be compiled before building extensions, but also when doing python setup.py --help
, for example. To avoid this, you could check for command line options, like suggested in https://github.com/scipy/scipy/blob/master/setup.py#L205, but on the other hand I don't really think it's worth the effort.
I found a very easy solution in [this post][1]:
Or you can stick to https://github.com/pypa/pip/issues/5761. Here you install cython and numpy using setuptools.dist before actual setup:
from setuptools import dist dist.Distribution().fetch_build_eggs(['Cython>=0.15.1', 'numpy>=1.10'])
Works well for me!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With