我希望向我的 Python setuptools 分布式包添加命名空间前缀。例如。我们有一个名为 common_utils 的包,并且希望将其作为伞.common_utils 进行访问,而不必在包树中包含虚拟目录/模块“umbrella”。
这可能吗?
谢谢
最佳答案
您可以使用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.pydevelop
目标有点像黑客。它将您的项目文件夹添加到 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'>
关于python - 有没有办法添加命名空间前缀 setuptools 包发行版?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6019042/