setup(name)和Extension(name)之间的关系是什么 [英] What is the relationship between setup(name) and Extension(name)

查看:128
本文介绍了setup(name)和Extension(name)之间的关系是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写(实际上生成)一个 setup.py 脚本,用于使用多个C ++源代码构建单个Python扩展。对我来说还不清楚,指定为 name 参数的名称与 distutils.core.setup 之间的关系是什么?指定为 distutils.core.Extension name 参数的名称。所以当我有这个时候:

I am writing (actually generating) a setup.py script for building a single Python extension with several C++ sources. It is unclear to me what is the relationship between the name specified as the name parameter to distutils.core.setup and the name specified as the name parameter to distutils.core.Extension. So when I have this:

distutils.core.setup(
  name = 'Abc',
  ext_modules = [
    distutils.core.Extension(
      name = 'Xyz',
      sources = ['a.cpp', 'b.cpp']
    )
  ]
)

Abc与之间有什么关系 Xyz ,尤其是在以下方面:

What is the relationship between Abc and Xyz, especially with respect to:


  • 其他

  • 用于在使用扩展名的Python脚本中导入扩展名的名称

  • 生成的的名称。 pyd (或 .so )文件

  • Each other
  • The name which will be used to import the extension in Python scripts which use it
  • The name of the generated .pyd (or .so) file

我在 distutils 的文档中阅读了此内容:

I read this in the documentation of distutils:


  • Abc 是程序包的名称

  • Xyz 是扩展名,包括任何软件包-即不是文件名或路径名,而是Python点缀名称

  • Abc is "The name of the package"
  • Xyz is "the full name of the extension, including any packages — ie. not a filename or pathname, but Python dotted name"

不幸的是,我无法解读我的答案由此(可能是因为Python不是我的主要语言,并且我只偶尔使用它。)

Unfortunately, I am not able to decipher my answer from this (possibly because Python is not my primary language and I only use it occasionally).

如果相关,我将创建一个CMake包装器以合并建筑物Python扩展进入了我项目的CMake框架。

In case it's relevant, the context is that I am creating a CMake wrapper to incorporate building Python extensions into my project's CMake framework.

推荐答案

最大的误解是 package一词的重载。游戏中有4种不同的名称-用于开发的目录名称(其中包含 setup.py ),目录名称包含 __ init __。py 和其他可导入模块,PyPI上的分发名称以及扩展名。前三个经常是相同或相似的,但这不是必需的。

The biggest source of misunderstanding is that the word "package" is heavily overloaded. There are 4 different names in the game — the name of the directory being used for development (the one that contains setup.py), the name of the directory contained __init__.py and other importable modules, the name of distribution at PyPI, and the name of the extension. Quite often the first 3 are the same or similar but that's not required.

开发目录的名称可以是任意名称,但不起作用。当然,正确命名是很方便的,但这只是方便。

The name of the development directory can be any, its name doesn't play any role. Of course it's convenient to name it properly but that's only convenience.

带有Python文件的目录名称为要导入的软件包命名。一旦软件包被命名为导入,名称通常会卡住并且无法更改。

The name of the directory with Python files name the package to be imported. Once the package is named for import the name usually stuck and cannot be changed.

分发版本的名称在PyPI上给出了一页,并且分发文件的名称(源分布,鸡蛋,车轮)。这是一个放在 setup(name ='distribution')调用中的名称。

The name of the distribution gives one a page at PyPI and the name of distribution files (source distribution, eggs, wheels). It's the name one puts in setup(name='distribution') call.

扩展名是python模块,因此扩展名必须是正确的模块名称。但是,在 Extension()中放置的名称必须是模块的完整python路径,包括顶级包名称。在您的示例中,是

Extensions are python modules so the name of an extension must be proper module names. But the name one puts in Extension() must be full python path to the module including the top-level package name. In your example it's

Extension(
  name = 'Abc.Xyz',
  sources = ['a.cpp', 'b.cpp']
)

这会产生扩展名可以导入为

This produces an extension that can be imported as

  import Abc.Xyz

  from Abc import Xyz

  from Abc.Xyz import <internal name>

让我显示详细的真实示例。我一直在维护一个名为 CheetahTemplate 的模板库。我在名为 cheetah3 / 的开发目录中进行开发。 PyPI上的发行版称为 Cheetah3 ;这是我在 setup(name ='Cheetah3') 。顶级模块是 Cheetah 因此,会从Cheetah导入Cheetah.Template 来导入模板;这意味着我有一个目录 cheetah3 / Cheetah /

Let me show detailed real example. I've been maintaining a templating library called CheetahTemplate. I develop it in the development directory called cheetah3/. The distribution at PyPI is called Cheetah3; this is the name I put into setup(name='Cheetah3'). The top-level module is Cheetah hence one does import Cheetah.Template or from Cheetah import Template; that means that I have a directory cheetah3/Cheetah/.

该库具有扩展名 _namemapper Extension()中使用的名称为 Cheetah._namemapper

The library has an extension _namemapper. The name used in Extension() is Cheetah._namemapper.

这篇关于setup(name)和Extension(name)之间的关系是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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