NumPy's setup.py imports the same package it is trying to install

Viewed 42

Taking a look at NumPy's setup.py I have noticed that at some point it includes something from NumPy's submodule numpy.distutils.

from numpy.distutils.core import numpy_cmdclass

I really cannot see how this can work, given that numpy.distutils – I think – should be part of the very same package it is trying to install. Wouldn't it be like if a package depends on itself to be installed? What's the working principle behind this? Why does this work?

I have noticed that it does the exact same thing, but from files that are actually part of the package. In this second case wouldn't it be better to use relative imports?

1 Answers

This is just Python's regular import mechanism at work. The first directory in the module search path is the "directory containing the input script", here setup.py. Packages are searched in the same way as modules.

So when setup.py is executed, it finds the numpy folder in its own directory. That folder contains a file named __init__.py which marks it as a regular package. The module loader then goes on to find the sub-package distutils (which is vendored in so that it isn't required to be installed already) and the module core.py.

Related