Right way to set python package with sub-packages
Just use setuptools
instead of distutils
, it has find_packages
exactly for that purpose:
from setuptools import setup, find_packagessetup( name='myPackage', packages=find_packages(),)
TL;DR: Nest the package in another package having the same name.
I nested the super-package myPackage
inside a directory (having the same name) as follow:
myPackage├── myPackage│ ├── __init__.py│ ├── mySubPackage1│ │ ├── foo1.py│ │ ├── foo2.py│ │ └── __init__.py│ └── mySubPackage2│ ├── bar1.py│ ├── bar2.py│ └── __init__.py└── setup.py
Then, I updated the setup.py
:
from distutils.core import setupif __name__ == "__main__": setup( name='myPackage', package_dir = { 'myPackage': 'myPackage', 'myPackage.mySubPackage1': 'myPackage/mySubPackage1', 'myPackage.mySubPackage2': 'myPackage/mySubPackage2'}, packages=['myPackage', 'myPackage.mySubPackage1', 'myPackage.mySubPackage2'] )
Now, sudo python setup.py install
behaves as I expect and in dist-packages
I have the following structure:
myPackage├── __init__.py├── __init__.pyc├── mySubPackage1│ ├── foo1.py│ ├── foo1.pyc│ ├── foo2.py│ ├── foo2.pyc│ ├── __init__.py│ └── __init__.pyc└── mySubPackage2 ├── bar1.py ├── bar1.pyc ├── bar2.py ├── bar2.pyc ├── __init__.py └── __init__.pyc
and an egg file.
This is almost good. Now it is not platform independent because of the usage of /
. To fix this, I edited setup.py
as follow:
from distutils.core import setupfrom distutils import utilif __name__ == "__main__": pathMySubPackage1 = util.convert_path('myPackage/mySubPackage1') pathMySubPackage2 = util.convert_path('myPackage/mySubPackage2') setup( name='myPackage', package_dir = { 'myPackage': 'myPackage', 'myPackage.mySubPackage1': pathMySubPackage1, 'myPackage.mySubPackage2': pathMySubPackage2}, packages=['myPackage', 'myPackage.mySubPackage1', 'myPackage.mySubPackage2'] )