有没有办法添加命名空间前缀 setuptools 包发行版?

发布于 2024-11-07 09:02:23 字数 153 浏览 0 评论 0原文

我希望向我的 Python setuptools 分布式包添加命名空间前缀。例如,我们有一个名为 common_utils 的包,并且希望将其作为伞.common_utils 进行访问,而不必在包树中包含虚拟目录/模块“umbrella”。

这可能吗?

谢谢,

I'm looking to add a namespace prefix to my Python setuptools distributed package. E.g. we have a package called common_utils and a would like it to be accessed as umbrella.common_utils without having to include the dummy directory/module 'umbrella' in the package tree.

Is this possible?

Thanks,

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

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

发布评论

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

评论(1

薄荷梦 2024-11-14 09:02:23

您可以使用 package_dir 选项告诉 setuptools 完整的包名称和子包的位置:

from setuptools import setup

setup(
    name = 'umbrella',
    packages = [
        'umbrella.common_utils'
        ],
    package_dir = {
        'umbrella.common_utils': './common_utils'
        }
    )

结果:

% python setup.py build
..
creating build/lib/umbrella
creating build/lib/umbrella/common_utils
copying ./common_utils/__init__.py -> build/lib/umbrella/common_utils

更新

正如您所发现的,python setup.py开发 目标有点像黑客。它将您的项目文件夹添加到 site-packages/easy-install.pth 中,但不执行任何操作来使您的包适应 setup.py 中描述的结构。不幸的是,我还没有找到与 setuptools/distribute 兼容的解决方法。

听起来您实际上想要这样的东西,您可以将其包含在项目的根目录中并根据您的需求进行自定义:

在项目根目录中创建名为 develop 的文件:

#!/usr/bin/env python

import os
from distutils import sysconfig

root = os.path.abspath(os.path.dirname(__file__))
pkg = os.path.join(sysconfig.get_python_lib(), 'umbrella')
if not os.path.exists(pkg):
    os.makedirs(pkg)
open(os.path.join(pkg, '__init__.py'), 'wb').write('\n')
for name in ('common_utils',):
    dst = os.path.join(pkg, name)
    if not os.path.exists(dst):
        os.symlink(os.path.join(root, name), dst)


(virt)% chmod 755 ./develop
(virt)% ./develop
(virt)% python -c 'from umbrella import common_utils; print common_utils'
<module 'umbrella.common_utils' from 
   '/home/pat/virt/lib/python2.6/site-packages/umbrella/common_utils/__init__.pyc'>

You can use the package_dir option to tell setuptools the full package name and location of the subpackage:

from setuptools import setup

setup(
    name = 'umbrella',
    packages = [
        'umbrella.common_utils'
        ],
    package_dir = {
        'umbrella.common_utils': './common_utils'
        }
    )

Result:

% python setup.py build
..
creating build/lib/umbrella
creating build/lib/umbrella/common_utils
copying ./common_utils/__init__.py -> build/lib/umbrella/common_utils

Updated

As you've discovered, the python setup.py develop target is a bit of a hack. It adds your project folder to site-packages/easy-install.pth, but does nothing to adapt your package to the structure described in setup.py. Unfortunately I have not found a setuptools/distribute-compatible workaround for this.

It sounds like you effectively want something like this, which you could include in the root of your project and customize to your needs:

Create file named develop in your project root:

#!/usr/bin/env python

import os
from distutils import sysconfig

root = os.path.abspath(os.path.dirname(__file__))
pkg = os.path.join(sysconfig.get_python_lib(), 'umbrella')
if not os.path.exists(pkg):
    os.makedirs(pkg)
open(os.path.join(pkg, '__init__.py'), 'wb').write('\n')
for name in ('common_utils',):
    dst = os.path.join(pkg, name)
    if not os.path.exists(dst):
        os.symlink(os.path.join(root, name), dst)


(virt)% chmod 755 ./develop
(virt)% ./develop
(virt)% python -c 'from umbrella import common_utils; print common_utils'
<module 'umbrella.common_utils' from 
   '/home/pat/virt/lib/python2.6/site-packages/umbrella/common_utils/__init__.pyc'>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文