0

I have a strange problem regarding import classes from subfolders.

I'm using Python 3.6, therefore an __init__.py should be not required in the subfolders.

I have the following file structure:

root
├── script.py (main)
└── custom
    ├── class1.py
    └── class2.py

This is script.py:

from custom.class1 import Class1
from custom.class2 import Class2

if __name__ == '__main__':
    cl1 = Class1()
    cl2 = Class2()

This is class1.py:

class Class1():
    def __init__(self):
        print('Class1')

if __name__ == '__main__':
    cl1 = Class1()

This is class2.py, which imports also class1:

from class1 import Class1

class Class2():
    def __init__(self):
        cl1 = Class1()
        print('Class2')

if __name__ == '__main__':
    cl2 = Class2()

And now the problem:

It works without error, when i am running python class1.py in the custom subfolder.

It works without error, when i am running python class2.py in the custom subfolder.

But when i am running python script.py in the root folder, i get the following error:

Traceback (most recent call last):
  File .... in <module>
    from custom.class2 import Class2
  File .... line 1, in <module>
    from class1 import Class1
ModuleNotFoundError: No module named 'class1'

How can this be fixed in a way, that the scripts in the custom subfolders can be run on its own and also the script in the root folder works?

Franky1
  • 402
  • 6
  • 15

1 Answers1

0

The problem is that you are running custom.class2 inside of script.py, which means while running custom.class2, you are still in the root directory.

To fix this, you should replace from class1 import Class1 from class2.py with from custom.class1 import Class1.

If you need to be able to run the file from any working directory, you may replace it's contents with this:

import os
import sys

path = os.path.abspath(os.path.dirname(__file__))
if not path in sys.path:
    sys.path.append(path)

from class1 import Class1

class Class2():
    def __init__(self):
        cl1 = Class1()
        print('Class2')

if __name__ == '__main__':
    cl2 = Class2()

The code adds the file's path in to the sys.path list, which holds different paths from which you can import modules.

NemPlayer
  • 786
  • 1
  • 11
  • 19
  • Thanks for the code example. Really annoying that python cannot handle this itself. I am wondering how to run unit tests with this path hell... – Franky1 Mar 02 '19 at 13:01
  • That shouldn't be too much of a problem as the current working directory isn't changing, it's going to be the same, it's just adding a path which it should check whenever a module can't be found. I'll notify you if I find a "cleaner" answer. – NemPlayer Mar 02 '19 at 13:17