4

my structure dictionary is

mainFolder    
    folder1
       __init__.py
       file1.py
       file2.py    
    folder2
       __init__.py
       file3.py
       file4.py    
    setup.py    
    __init__.py

i need import file4.py from folder2 to folder1/file1.py
file1.py:

from ..folder2.file4 import MyClass

and i gets:

SystemError: Parent module '' not loaded, cannot perform relative import

how to fix that ?

5
  • setup should be outside your top package, not in it. Commented Dec 11, 2017 at 19:12
  • Show how you run the script. Commented Dec 11, 2017 at 19:13
  • Possible duplicate of Relative imports for the billionth time Commented Dec 11, 2017 at 19:14
  • @MadPhysicist python3 folder1/file1.py i want run only this file Commented Dec 11, 2017 at 19:14
  • 1
    python -m folder1.file1 from the main folder. Or python -m mainFolder.folder1.file1, depending on how you really want to structure this thing. You currently have a bit of a mess with the setup.py Commented Dec 11, 2017 at 19:15

1 Answer 1

7

This is because you must to explicitly name the parent package. So in your case you need either from mainFolder.folder2.file4 import Myclass, either from folder2.file4 import Myclass

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.