2

I need help setting up my directory structure for a python application. It is a command line application (using Click, if that matters).

My directory structure looks like this:

mycli
├── mycli
│   ├── cli.py
│   ├── __init__.py
│   └── mycli.py
└── setup.py

In mycli/mycli/__init__.py I have one line of code:

__application__ = "my-cli"

In mycli/mycli/cli.py I have this:

from mycli import __application__
import click
import sys

@click.group()
def my_cli():
    """ rest of my code """

if __name__ == "__main__":
    sys.exit(my_cli())

Finally, in my setup.py, I have added an entry point so that I can use this application on the command line:

entry_points={
    'console_scripts': [
        'my-cli=mycli.cli:my_cli',
    ],
},

I can install my application using pip install -e . and then execute my-cli as expected. This works.

The problem I am having is that I want to run python mycli/cli.py as well. This is useful for debugging purposes. It fails to read the __application__ though:

$ python mycli/cli.py 
Traceback (most recent call last):
  File "mycli/cli.py", line 1, in <module>
    from mycli import __application__
ImportError: cannot import name '__application__'

What do I need to do with my project layout so that I can continue to use the entry_points in setup.py AND be able to run my application with python mycli/cli.py?

I tried to change the import to

from .mycli import __application__

But I receive these errors:

When running python mycli/cli.py

ModuleNotFoundError: No module named '__main__.mycli'; __main__ is not a package

The entry_point breaks with:

ImportError: cannot import name '__application__'
3
  • Try from .mycli import __application__ Commented May 9, 2019 at 14:06
  • @Sparky05 That fails with ModuleNotFoundError: No module named __main__.mycli'; __main__ is not a package when I run python mycli/cli.py and also breaks the entry_point with ImportError: cannot import name '__application__' Commented May 9, 2019 at 14:49
  • Have you tried moving the mycli.py one folder up or import directly from the file __application__(e.g. from .cli import __application__) is inside? Commented May 9, 2019 at 15:27

1 Answer 1

2

Your problem is a conflicting namespace. Your program has the same name as your module. So:

from mycli import __application__ 

tries to import from mycli.py, not from mycli/__init__.py. I suggest you rename your script to my-cli.py.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.