Search code examples
pythonsetuptoolsdistutilspip

Python: pip installs sub-packages in root dir


I have such structure:

setup.py
package
    __init__.py
    sub_package
        ___init__.py
    sub_package2
        __init__.py

If I install package via setup.py install, then it works as appreciated (by copying whole package to site-packages dir):

site_packages
    package
        sub_package
        sub_package2

But if I run pip install package, then pip installs each sub-package as independent package:

site-packages
    package
    sub_package
    sub_package2

How can I avoid this? I use find_packages() from setuptools to specify packages.


Solution

  • NOTE: This answer is not valid anymore, it's only kept for historical reasons, the right answer right now is to use setuptools, more info https://mail.python.org/pipermail/distutils-sig/2013-March/020126.html


    First of all i will recommend to drop setuptools :

    alt text

    And use either distutils (which is the standard mechanism to distribute Python packages) or distribute you have also distutils2 but i think is not ready yet, and for the new standard here is a guide line to how to write a setup.py.

    For your problem the find_packages() don't exist in the distutils and you will have to add your package like this:

    setup(name='package',
          version='0.0dev1',
          description='blalal',
          author='me',
          packages=['package', 'package.sub_package', 'package.sub_package2'])
    

    And if you have a lot of package and sub packages you will have to make some code that create the list of packages here is an example from Django source.

    I think using distutils can help you with your problem,and i hope this can help :)