开发者

Is there a way to add a namespace prefix setuptools package distributions?

开发者 https://www.devze.com 2023-03-06 18:15 出处:网络
I\'m looking to开发者_如何学C 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

I'm looking to开发者_如何学C 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,


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'>
0

精彩评论

暂无评论...
验证码 换一张
取 消