Setuptools软件包发现

发布于 2025-02-10 07:16:22 字数 633 浏览 0 评论 0原文

我的项目注释下面有下面的包装树


└── src
|    ├── mypkg1
|    │   ├── module1.py
|    │   ├── module2.py
|    │   └── __init__.py
|    |   |-- setup.py
|    |   |__ requirements.py 
|    └── mypkg2
|        ├── module1.py
|        └── __init__.p
|
|---- base
|      |----- init.py
|      |----- module1.py      

-SRC和基础处于同一级别。

我应该如何配置setup.py文件,以便我可以仅使用basemypkg1 packages,并排除mypkg2

我已经尝试提及['base','mypkg1'] in packages中,但没有使用find_packages()它将仅显示mypkg1 < /code>和mypkg2

I have below package tree for my project


└── src
|    ├── mypkg1
|    │   ├── module1.py
|    │   ├── module2.py
|    │   └── __init__.py
|    |   |-- setup.py
|    |   |__ requirements.py 
|    └── mypkg2
|        ├── module1.py
|        └── __init__.p
|
|---- base
|      |----- init.py
|      |----- module1.py      

Note - src and base are at same level.

How should I configure setup.py file, so that I can install base and mypkg1 packages only and exclude mypkg2.

I have tried mentioning ['base','mypkg1'] in packages but didn't worked also tried using find_packages() it will show up only mypkg1 and mypkg2.

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

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

发布评论

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

评论(1

め可乐爱微笑 2025-02-17 07:16:22

由于复杂的代码库布局,有很多事情要处理。另外,我不确定为什么您要在src/mypkg1中显示设置脚本 - 从那里调用它会找不到src之外的任何东西,因此您将拥有没有机会包括base。在下面的示例中,我切换到包含srcbase dirs的根目录:

project_root
├── src/
├── base/
└── setup.py

base发现,

因为base在项目root下,将通过plain find_packages调用来发现:

>>> from setuptools import find_packages
>>> find_packages()
['base']

要在src下查找软件包,请通过 where 参数:

>>> from setuptools import find_packages
>>> find_packages(where='src')
['mypkg1', 'mypkg2']

排除mypkg2 ,通过排除参数中的列表将其传递:

>>> from setuptools import find_packages
>>> find_packages(where='src', exclude=['mypkg2'])
['mypkg1']

软件包结构映射和文件包含

调用setuptools.setup()与收集的软件包一样,仅适用于>基本,因为mypkg1不在根级别上。您必须通过package_dir映射其位置:

setup(
    ...,
    package_dir={'': 'src'},
)

但是,这是指令setup() src中查找 all> All packages的文件,在base下制作文件。因此,您必须明确地映射:

setup(
    ...,
    package_dir={'': 'src', 'base': 'base'},
)

现在完成最小设置脚本,

from setuptools import setup, find_packages

setup(
    name='...',
    version='...',
    packages=find_packages() + find_packages(where='src', exclude=['mypkg2']),
    package_dir={'': 'src', 'base': 'base'},
)

例如构建车轮时,这将产生以下文件布局:

  Length      Date    Time    Name
---------  ---------- -----   ----
        0  06-24-2022 21:55   base/__init__.py
        0  06-24-2022 21:55   base/module1.py
        0  06-24-2022 21:54   mypkg1/__init__.py
        0  06-24-2022 21:54   mypkg1/module1.py
        0  06-24-2022 21:54   mypkg1/module2.py

There are multiple things to take care of due to the complex codebase layout. Also, I'm not sure why you are showing the setup script inside src/mypkg1 - invoking it from there will not find anything outside of src and thus you'll have no chance to include base. In the below example, I switched to the root directory containing both src and base dirs:

project_root
├── src/
├── base/
└── setup.py

Package discovery

Since base is under the project root, it will be discovered by plain find_packages invocation:

>>> from setuptools import find_packages
>>> find_packages()
['base']

To find packages under src, pass it via where argument:

>>> from setuptools import find_packages
>>> find_packages(where='src')
['mypkg1', 'mypkg2']

To exclude mypkg2, pass it via the list in exclude argument:

>>> from setuptools import find_packages
>>> find_packages(where='src', exclude=['mypkg2'])
['mypkg1']

Package structure mapping and file inclusion

Calling setuptools.setup() as is with the collected packages will only work for base, since mypkg1 is not on the root level. You have to map its location via package_dir:

setup(
    ...,
    package_dir={'': 'src'},
)

This, however, instructs setup() to look for files of all packages in src, which makes files under base not findable. You thus have to map it explicitly:

setup(
    ...,
    package_dir={'': 'src', 'base': 'base'},
)

Complete minimal setup script

from setuptools import setup, find_packages

setup(
    name='...',
    version='...',
    packages=find_packages() + find_packages(where='src', exclude=['mypkg2']),
    package_dir={'': 'src', 'base': 'base'},
)

Now when e.g. building a wheel, this will produce the following file layout:

  Length      Date    Time    Name
---------  ---------- -----   ----
        0  06-24-2022 21:55   base/__init__.py
        0  06-24-2022 21:55   base/module1.py
        0  06-24-2022 21:54   mypkg1/__init__.py
        0  06-24-2022 21:54   mypkg1/module1.py
        0  06-24-2022 21:54   mypkg1/module2.py
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文