Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to include sub-packages in setup.py

I have a python package called mltester which contains two sub-packages (actions, dialogs) and a main script ml_tester.py, structured as follows:

+ <ProjectFolder>
+---+ <mltester>
|   +---- <actions>
|   +---- <dialogs>
|   +---- ml_tester.py
|   +---- __init__.py
+---- setup.py

My __init__.py looks as follows:

import actions
import dialogs
import ml_tester

In ml_tester.py I do something like:

from actions import *
from dialogs import *

All works fine when running from eclipse. When doing pip install, the following setup.py works fine:

from setuptools import setup
setup(
    name="MLTester",
    version="1.0",
    packages=["mltester",
              "mltester.actions",
              "mltester.dialogs"],
    install_requires=[
        "matplotlib",
    ],
    entry_points='''
        [console_scripts]
        ml_tester_gui=mltester.ml_tester:main
    '''
)

But when I remove "mltester.actions", "mltester.dialogs" from the list of packages, I now get an error like:

File "/usr/local/lib/python2.7/dist-packages/mltester/__init__.py", line 1, in <module>
    import actions
ImportError: No module named actions

And I don't understand why listing just the containing mltester package is not enough. Of Course I can simply add the packages back, but now I think that I'm missing something more conceptual here.

like image 419
Elad Weiss Avatar asked Apr 29 '18 05:04

Elad Weiss


People also ask

What is sub package in Python?

A Python package in simple words is a directory that contains Python files. Just like a directory has sub-directories and those sub-directories also have files inside, a Python package also has sub-packages and those sub-packages again have different modules defined inside.

Why do we need packages in Python?

Use of packages helps importing any modules, individually or whole. While importing a package or sub packages or modules, Python searches the whole tree of directories looking for the particular package and proceeds systematically as programmed by the dot operator.

What should setup py contain?

The setup.py file may be the most significant file that should be placed at the root of the Python project directory. It primarily serves two purposes: It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on.

What are the package and sub packages in Python?

The four modules ( mod1.py , mod2.py , mod3.py , and mod4.py ) are defined as they were before. But now, instead of being lumped together into the pkg directory, they are split out into two subpackage directories: sub_pkg1 and sub_pkg2 . Or you can use a relative import, where ..


1 Answers

Because packages do not do any package lookup in subtree. Adding a package to packages will only include the package itself and all direct submodules, but none of the subpackages.

For example, if you have a source tree with package spam containing a module eggs and subpackage bacon:

src
└── spam
    ├── __init__.py
    ├── eggs.py
    └── bacon
        └── __init__.py

Specifying packages=['spam'] will include only spam and spam.eggs, but not spam.bacon, so spam.bacon will not be installed. You have to add it separately to include the complete source codebase: packages=['spam', 'spam.bacon'].

To automate the building of the packages list, setuptools offers a handy function find_packages:

from setuptools import find_packages, setup

setup(
    packages=find_packages(),
    ...
)
like image 140
hoefling Avatar answered Oct 14 '22 16:10

hoefling