1

Im getting a message from pycodestyles in VisualStudio that sais 'module level import not at top of file'. This is because, in my company, there is this big project that looks like this:

  • Main Folder
    • Project1
    • Project2
      • Utils
        • Utils.py
      • MyModule
        • MyWorkingDirectory
          • Myfile.py
      • AnotherModule

So I need to use functions in the Utils.py that are going to be shared from different modules (MyModule & AnotherModule in this case). I have been reading a lot about python imports (stackoverflow is full of this kind of questions, maybe we should start thinking this is a not very friendly way to do it..) so I did this:

import sys
sys.path.append(str(Path(__file__).resolve().parents[2]))
from utils.utils import *

My question is: is there a good way of doing this without restructuring the project? I have read a lot of workarounds, but I would like to know if there is a common-standard-good-way of resolving this.

I'm using Python 3.10.7.

Thanks in advance

CM35
  • 11
  • 1
  • This question & its accepted answer has a lot of useful discussions: https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time. Basically you need to take into consideration how you invoke Myfile.py. – Tony Beta Lambda Sep 20 '22 at 16:41

0 Answers0