Python 扩展未安装到子包中

发布于 2024-12-23 02:31:12 字数 859 浏览 4 评论 0原文

我正在尝试构建一个 Python 扩展并使用 distutils 将其打包,但无论我如何命名它,该扩展都会安装在根包中。我的目录布局如下所示:

foo/bar/extension.c

我的 setup.py 如下所示:

from distutils.core import setup
from distutils.extension import Extension

setup(name='foo.bar.extension',
      cmdclass={'build_ext': build_ext},
      ext_modules=[Extension('foo.bar.extension',
                             sources=['foo/bar/extension.c'])]
)

我设置了一个 virtualenv

python setup.py install

在我的 Python shell 中运行 然后:

>>> import foo.bar.extension
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named invest_cython_core
>>> import extension #This works!

我可以更改什么以便第一个import 语句有效,而第二个语句失败?

I'm trying to build a Python extension and package it up using distutils but the extension installs in the root package no matter how I name it. My directory layout looks like this:

foo/bar/extension.c

My setup.py looks like this:

from distutils.core import setup
from distutils.extension import Extension

setup(name='foo.bar.extension',
      cmdclass={'build_ext': build_ext},
      ext_modules=[Extension('foo.bar.extension',
                             sources=['foo/bar/extension.c'])]
)

I set up a virtualenv and run

python setup.py install

Then in my Python shell:

>>> import foo.bar.extension
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named invest_cython_core
>>> import extension #This works!

What can I change so that the first import statement to works and the second one to fails?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2024-12-30 02:31:12

我认为你需要有 foo/__init__.pyfoo/bar/__init__.py 以便 distutils 首先安装这些包,然后安装扩展模块。 (这里的错误比无声的不当行为要好,我将打开一个错误报告,以便 distutils2 表现得更好。)

您是否使用自定义的 build_ext 类? (在您的示例中询问是因为 cmdclass={'build_ext': build_ext} )这可能会导致问题。

I think you need to have foo/__init__.py and foo/bar/__init__.py so that distutils installs first these packages and then the extension module. (An error would be better than a silent misbehavior here, I shall open a bug report so that distutils2 behaves better.)

Are you using a custom build_ext class? (asking because of cmdclass={'build_ext': build_ext} in your example) That may play a part in the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文