有没有办法给setuptools包发行版添加命名空间前缀?
我想给我的Python setuptools发布的包加个命名空间前缀。比如,我们有一个叫做common_utils的包,我希望它能被访问为umbrella.common_utils,而不需要在包的结构里包含一个虚假的目录或模块'umbrella'。
这样做可以吗?
谢谢!
1 个回答
3
你可以使用 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 develop
这个命令有点像是个小技巧。它会把你的项目文件夹添加到 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'>