4

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!

2
  • Probably wrong imports; please add the contents of the __main__.py to the question. Commented May 28, 2020 at 7:57
  • 1
    What are exactly the commands that you call, and what are the full error messages that are generated? What are the commands you used to package the project and to install it? You mention a console script entry point do_something = MyPackage.__main__:main, but there doesn't appear to be any main callable (function) in the MyPackage/__main__.py file. Commented Jun 5, 2020 at 21:43

2 Answers 2

5
+25

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 
Sign up to request clarification or add additional context in comments.

Comments

2

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.