How to make setup.py for standalone python application in a right way?

With that directory (package) structure, in your app.py you should import as one of the following:

from app.aaa.module_a import method1
from .aaa.module_a import method1

Then make sure to call you application like one of the following:

app

(this should work thanks to the console entry point)

python -m app.app

(this should work even without the console entry point)


I try to recreate the complete project here

Directory structure:

.
├── app
│   ├── aaa
│   │   └── module_a.py
│   ├── app.py
│   └── bbb
│       └── module_b.py
└── setup.py

setup.py

import setuptools

setuptools.setup(
    name="app",
    version="1.0",
    packages=['app', 'app.aaa', 'app.bbb'],
    entry_points={
        'console_scripts': ['app=app.app:main', ]
    },
)

app/app.py

#!/usr/bin/python

from .aaa.module_a import method1
from .bbb.module_b import method2

def main():
    print("APP main executed")
    method1()
    method2()

if __name__ == '__main__':
    main()

app/aaa/module_a.py

def method1():
    print("A1 executed")

app/bbb/module_b.py

def method2():
    print("B2 executed")

Then I run following commands:

$ python3 -V
Python 3.6.9
$ python3 -m venv .venv
$ .venv/bin/python -m pip install -U pip setuptools wheel
# [...]
$ .venv/bin/python -m pip list
Package       Version
------------- -------------------
pip           20.3.3
pkg-resources 0.0.0
setuptools    51.1.0.post20201221
wheel         0.36.2
$ .venv/bin/python -m pip install .
# [...]
$ .venv/bin/python -m app.app
APP main executed
A1 executed
B2 executed
$ .venv/bin/app
APP main executed
A1 executed
B2 executed
$ .venv/bin/python -m pip uninstall app
# [...]
$ .venv/bin/python -m pip install --editable .
# [...]
$ .venv/bin/python -m app.app
APP main executed
A1 executed
B2 executed
$ .venv/bin/app
APP main executed
A1 executed
B2 executed