0

I'm trying to import script1.py from script2.py but getting this error:

ImportError: attempted relative import with no known parent package

Parent_Dir/
    __init__.py
    Child1/
        script1.py
    Child2/
        __init__.py 
        script2.py 
    

I've tried a lot of ways to do this but none of them worked for me. This is what I've tried:

from .Child1 import script1

from ..Child1 import script1

from .Parent_Dir.Child1 import script1

from Parent_Dir.Child1 import script1

I know there are a lot of similar questions on SOF but most of them are either using sys or os or importlib or PYTHONPATH. Is there any way to do this without using any module and without using path or PYTHONPATH ( I mean by using import statements only).

Devansh Soni
  • 771
  • 5
  • 16
  • (https://stackoverflow.com/questions/4383571/importing-files-from-different-folder) see this link. – liamsuma Aug 18 '20 at 15:27
  • Why are `script1` and `script2` inside a package? If they are literally scripts (as opposed to modules), they should be in the top-level directory (i.e. the one that contains `Parent_Dir`). Any functionality required by both scripts should go in the modules inside the package. Then the imports will all work as expected. – ekhumoro Aug 18 '20 at 15:33
  • @liamsuma I've tried these answers (https://stackoverflow.com/a/42170807/12566054, https://stackoverflow.com/a/21995949/12566054) but they didn't work for me. – Devansh Soni Aug 18 '20 at 16:06

0 Answers0