PythonSlacker
PythonSlacker

Reputation: 61

Python: multiple packages with multiple setup.py files

I'm having a hard time constructing my Python setup.py files to do what I want. I have one pacakge set up like this:

somestuff_root/
    setup.py
    myutils/
        __init__.py
        a/
            __init__.py
            somestuff.py

I have another package setup like this:

otherstuff_root/
    setup.py
    myutils/
        __init__.py
        b/
            __init__.py
            otherstuff.py

so things are organized in my site-packages/ directory like:

myutils/
    a/
        somestuff.py
    b/
        otherstuff.py

which is exactly what I want after installing them both with pip.

My problem is that uninstalling the second package (with pip) also wipes out the first one -- this is not what I want to happen. I would like it just to remove myutils.b and keep myutils.a where it is.

I suspect I'm confusing things with having multiple init.py files in with myutils/ folders, but I'm not sure how else to get these to work properly.

--

Also found this helpful page:

http://www.sourceweaver.com/musings/posts/python-namespace-packages

Upvotes: 6

Views: 11134

Answers (1)

Erik Youngren
Erik Youngren

Reputation: 811

If I'm understanding this correctly, what you are trying to set up is a namespace package (an empty package that contains other, separately installed packages), which is a feature of setuptools.

Call setuptools.setup() with a list of packages that are namespaces for the namespace_packages argument.

setup(..., namespace_packages=['myutils'], ...)

Then, create myutils/__init__.py containing only the following:

__import__('pkg_resources').declare_namespace(__name__)

Finally, in myutils/a/__init__.py and myutils/b/__init__.py call pkg_resources.declare_namespace('myutils'), which ensures that the namespace is created if a lower-level package is installed first.

I'm pretty sure that's how it works. I'm still learning setuptools so if I'm wrong, corrections are much appreciated.

Upvotes: 10

Related Questions