The command `python setup.py build_ext --inplace` always create a new directory

青春壹個敷衍的年華 提交于 2019-11-28 06:15:22

问题


Suppose I have a python package structured like this:

foo/
  __init__.py
  setup.py
  bar/
    __init__.py
    bar.pyx

and the content of setup.py is

from distutils.core import setup
from Cython.Build import cythonize
import numpy as np

setup(
    ext_modules=cythonize("bar/bar.pyx"),
    include_dirs=[np.get_include()]
)

Then I just run

python setup.py build_ext --inplace

because I need the compiled file bar.so be placed exactly in bar/. But the previous command creates a new directory foo/bar/ under bar, and put bar.so there, say,

foo/
  __init__.py
  setup.py
  bar/
    __init__.py
    bar.pyx
    foo/
      bar/
        bar.so

while what I need is

foo/
  __init__.py
  setup.py
  bar/
    __init__.py
    bar.pyx
    bar.so

These annoying things happened after I turned foo and bar to be a package. If I remove foo/__init__.py and bar/__init__.py then bar.so would appear in foo/, still not foo/bar/. I've read the manual but haven't find a option to handle this problem.

So what should I do if I require bar.so to appear in the right place, while keeping the two __init__.py file?


回答1:


setup.py shouldn't live inside a package. You'll want to move the package one directory up:

foo/
  setup.py
  foo/
    __init__.py
    bar/
      __init__.py
      bar.pyx

This is the structure followed by the majority of packages I've come across.


As for scikit-learn, which you have used for inspiration:

I certainly don't know exactly what scikit-learn is doing with their multiple setup.py files, but I would wager a guess and say that the outer setup.py is using (calling, importing) the other setup.pys, to hand out the details of the configuration of a subpackage to those individual setup.pys. It's just not very visible, because (I think) distutils/setuptools is doing a lot of this importing under the hood.

Thus, the outer setup takes care of the package as a whole, the inner ones take care of the nitty gritty for a subpackage.

But in the end, it's still one big setup.py file in the other layer. Trying running

python setup.py build_ext --inplace

on any of the inner setup.pys and it will either fail, or do nothing (at least for the two attempts I gave it).

So, those subpackage setup.pys could likely be more appropriately seen as setup_config.py files.

As an interesting result of this, installing the package will keep those setup.py files in their respective subpackages (as well as the one in sklearn). Perhaps there's some use for that when one uses the packages, but I guess it's just an artefact of the scikit-learn setup procedure and the inclusion of all *.py files.



来源:https://stackoverflow.com/questions/31847561/the-command-python-setup-py-build-ext-inplace-always-create-a-new-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!