命令`python setup.py build_ext --inplace`总是创建一个新目录 [英] The command `python setup.py build_ext --inplace` always create a new directory

查看:157
本文介绍了命令`python setup.py build_ext --inplace`总是创建一个新目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个结构如下的python包:

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

,而 setup.py 的内容来自distutils

 。来自Cython的核心导入设置
。构建导入cythonize
导入numpy为np

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

然后我刚刚运行

  python setup.py build_ext --inplace 

因为我需要将编译后的文件 bar.so 准确放置在 bar / 。但是上一条命令在 bar 下创建了一个新目录 foo / bar / ,并将 bar .so

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

而我需要的是

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

这些令人讨厌的事情发生在我打开 foo 之后bar 打包。如果我删除 foo / __ init __。py bar / __ init __。py 然后 bar.so 将出现在 foo / 中,但不会出现在 foo / bar / 中。我已经阅读了手册,但没有找到解决此问题的选项。



所以,如果我需要 bar.so怎么办? 出现在正确的位置,同时保留两个 __ init __。py 文件?

解决方案

setup.py 不应放在程序包中。您需要将软件包上移一个目录:

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

这是我遇到的大多数软件包的结构。






对于scikit-learn,您已经从中得到了启发:



我当然不知道scikit-learn在多个设置下到底在做什么.py 文件,但我会猜测一下,说外面的 setup.py 正在使用(调用,导入)另一个 setup.py s,以将子软件包配置的详细信息分发给那些单独的 setup.py s。它只是不太明显,因为(我认为)distutils / setuptools在后台进行了大量此类导入。



因此,外部设置负责整个程序包,内部设置负责子程序包的细节。



但是最后,它仍然是另一层的大 setup.py 文件。尝试运行

  python setup.py build_ext --inplace 

在任何内部 setup.py 上都会失败,或者什么都不做(至少对两个



因此,那些子包 setup.py s可能更恰当地视为 setup_config.py 文件。



一个有趣的结果是,安装软件包将保留那些 setup.py 文件放在各自的子包中(以及 sklearn 中的子包中)。也许当使用这些软件包时会有一些用处,但是我想这只是scikit-learn设置过程的产物,并且包含了所有 *。py 文件。 / p>

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?

解决方案

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.

这篇关于命令`python setup.py build_ext --inplace`总是创建一个新目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆