简体   繁体   English

Python:使用setuptools将文件/模块分发到另一个包中

[英]Python: Distributing a file/module into another package with setuptools

Generically, I'm trying to distribute a module/file that I want to be part of a another python package that has already set up a directory structure under site-packages. 通常,我试图分发一个模块/文件,使其成为另一个python程序包的一部分,该程序包已经在site-packages下设置了目录结构。

For the given structure under site-packages: 对于site-packages下的给定结构:

-- main_package
   -- __init__.py
   -- sub_package
      -- __init__.py
      -- bar.py

I have a module foo.py that I want to exist next to the module bar.py , both under the package sub_package . 我有一个模块foo.py ,我想存在旁边的模块bar.py ,都包下sub_package If I just setup the structure in my repo along the lines of a typical package then the __init__.py that setuptools requires clobber the existing __init__.py files for the main_package and sub_package . 如果仅按照典型软件包的方式在存储库中设置结构,则setuptools的__init__.py需要main_packagesub_package的现有__init__.py文件。 Ideally this would be a wheel or sdist that could be distributed from our own pypi. 理想情况下,这可能是可以从我们自己的pypi分发的wheel或sdist。

I've tried using py_module , but I can't get it to place the module anywhere other than the top level under site-packages. 我尝试使用py_module ,但无法将其放置在站点包下顶层以外的任何其他位置。

Specifically, I'm trying to distribute a saltstack external_pillar into the salt/pillar/ structure already setup by salt. 具体来说,我正在尝试将saltstack external_pillar分配到已经由salt设置的salt / pillar /结构中。 There are multiple salt instances that may or may not want the external_pillar, so simply bundling up the pillar into the salt distribution we have isn't feasible. 有多个盐实例可能需要也可能不需要external_pillar,因此仅将支柱捆绑到我们拥有的盐分布中是不可行的。

You may be looking for namespace packages . 您可能正在寻找名称空间包

With in your case a structure like this: 在您的情况下,具有如下结构:

`-- main_package
   |  # no __init__.py here
   `-- sub_package
      |-- __init__.py
      `-- foo.py

Python 3.3 added implicit namespace packages from PEP 420. All that is required to create a native namespace package is that you just omit __init__.py from the namespace package directory. Python 3.3从PEP 420添加了隐式名称空间包。创建本机名称空间包所需__init__.py只是从名称空间包目录中省略__init__.py

... ...

It is extremely important that every distribution that uses the namespace package omits the __init__.py or uses a pkgutil-style __init__.py . 极为重要的是,每个使用名称空间包的发行版都将忽略__init__.py或使用pkgutil样式的__init__.py If any distribution does not, it will cause the namespace logic to fail and the other sub-packages will not be importable. 如果没有分发,它将导致名称空间逻辑失败,并且其他子包将不可导入。

... ...

Finally, every distribution must provide the namespace_packages argument to setup() in setup.py. 最后,每个发行版都必须在setup.py中为setup()提供namespace_packages参数。

However, from your question I am not sure whether sub_package already exists in main_package or not. 但是,从您的问题中,我不确定sub_package是否已经存在main_package

If it does, I am not sure of the structure to use to extend sub_package while not shadowing its modules, you may need to use an alternate sub-package name to achieve this. 如果是这样,我不确定在不隐藏其模块的情况下用于扩展sub_package的结构,您可能需要使用备用子包名称来实现此目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM