Alex
Alex

Reputation: 115

Python package distribution with entry_point console_script

I am preparing to deploy a Python package with the following layout:

MyPackage/
  setup.py
  MyPackage/
    __init__.py
    __main__.py
    lib/
      __init__.py
      utils.py
    db/
      __init__.py
      db1.py
      db2.py 
    tasks/
      __init__.py
      something.py

The setup.py contains:

setup(name = MyPackage,
      ...
      packages = find_packages(),
      include_package_data = True,
      entry_points = {"console_scripts" : [
          "do_something = MyPackage.__main__:main"
      ]})

__main__.py contains:

import tasks.something as something

something.my_function()

something module contains:

import db.db1 as db1
import db.db2 as db2

def my_function():
  db1.this_func(...)
  db2.that_func(...)

db1 contains:

import sqlalchemy
import lib.utils as utils

def this_func(...):
  sqlalchemy.create_engine(...)

and db2 contains:

import sqlalchemy
import lib.utils as utils

def that_func(...):
  sqlalchemy.create_engine(...)

When run by executing from the installed site-packages directory, __main__.py completes without issue. If I perform import MyPackage.tasks.something as something in an interactive session, the module also imports with issue. When I run the console_scripts script do_something, however, I receive ModuleNotFound errors for tasks.

What is causing my console script to fail to locate the subpackages?

Thanks in advance!

Upvotes: 4

Views: 684

Answers (2)

wim
wim

Reputation: 362826

Two problems here.

  1. Import statements in __main__.py were implicit relative. Change them to absolute imports.
  2. A console_scripts entry point target needs to resolve to a callable which accepts no arguments. In your case, that means the module MyPackage/__main__.py should define a callable main.

Change __main__.py like this:

from MyPackage.tasks import something

def main():
    # command line arguments parsing and logging configuration goes here ..
    something.my_function()

if __name__ == "__main__":
    main()

Similarly, you should change the import statements in db1, db2, and something into correct relative imports:

from ..db import db1
from ..db import db2
from ..lib import utils

Alternatively, use absolute imports:

from MyPackage.db import db1
from MyPackage.db import db2
from MyPackage.lib import utils

Upvotes: 2

dominik
dominik

Reputation: 189

Change your imports, following this example:

__main__.py:

import tasks.something as something

# to:

import MyPackage.tasks.something as something

# or

from .tasks import something as something

Upvotes: 5

Related Questions