0

My general Problem is following: I have a package with two modules inside with one of them using the other and I want to debug the module. Here a short example:

main.py
package1/
    __init__.py
    module1.py
    module2.py

The problems started with the cannot perform relative import Error and I read through this Relative imports in Python 3. The examples of the first awnser are working, but they have some problems themself. I am using VSC with pylint all Warnings, Errors, Hints, etc enabled and try to fix all of them.

example 1

Using the direct call if the module is unique from module2 import class_name, works fine, but results in the problem that pylance isn't able to find it and is linting it.

example 2

putting it on the path results in another Problem, which results in a pep warning. So pep doesn't like it if the imports are not on top of the file.

import sys
import os

PACKAGE_PARENT = '..'
SCRIPT_DIR = os.path.dirname(os.path.realpath(os.path.join(os.getcwd(), os.path.expanduser(__file__))))
sys.path.append(os.path.normpath(os.path.join(SCRIPT_DIR, PACKAGE_PARENT)))

from package1.module2 import class_name

What I am looking for

So it seems from my perspective, the call of

if __name__ == "__main__":
    class_name()

inside a Module is a anti-pattern, but a great way to debug a module very easy. What is the correct way to create this structure and get rid of the warnings? Should I remove the class calls inside the module and create debug scripts for all of them?

MaKaNu
  • 502
  • 3
  • 19

0 Answers0