0

I use Python 2.7. I'm trying to run my UI-automation script, but I got ImportError.

I have at least 30 Classes with methods. I want to have these methods in each and any class that's why I created BaseClass(MainClass) and created objects of all my classes. Please advise what should I do in this case or how I can solve this problem.

Here the example what similar to my code.

test_class/baseclass.py

from test_class.first_class import FirstClass
from test_class.second_class import SecondClass


class MainClass:
    def __init__(self):
        self.firstclass = FirstClass()
        self.secondclass = SecondClass()

test_class/first_class.py

from test_class.baseclass import MainClass

class FirstClass(MainClass):
    def __init__(self):
        MainClass.__init__(self)

    def add_two_number(self):
        return 2 + 2

test_class/second_class.py

from test_class.baseclass import MainClass

class SecondClass(MainClass):
    def __init__(self):
        MainClass.__init__(self)

    def minus_number(self):
        return self.firstclass.add_two_number() - 10


if __name__ == '__main__':
    print(SecondClass().minus_number())

When I run the last file I get this error

Traceback (most recent call last):
  File "/Users/nik-edcast/git/ui-automation/test_class/second_class.py", line 1, in <module>
    from test_class.baseclass import MainClass
  File "/Users/nik-edcast/git/ui-automation/test_class/baseclass.py", line 1, in <module>
    from test_class.first_class import FirstClass
  File "/Users/nik-edcast/git/ui-automation/test_class/first_class.py", line 1, in <module>
    from test_class.baseclass import MainClass
ImportError: cannot import name MainClass
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • This seems caused by circular import, this may be a dupe of [this thread](https://stackoverflow.com/questions/22187279/python-circular-importing) – Rieljun Liguid May 13 '18 at 16:58

3 Answers3

1

check this line: from test_class.baseclass import MainClass -> it seems like all other imports had a '_' between the names like second_class. So try maybe to write base_class. who knows might work

Elad Goldenberg
  • 99
  • 1
  • 10
0

Are you proabably running your code like python test_class/second_class.py. If you just do this then python will think the base directory to find modules is ./test_class. So when you import the test_class package python will start looking for a folder called ./test_class/test_class to find the sub-modules. This directory doesn't exist and so the import fails. There are several ways that you can tell python how to correctly find your modules.

Using PYTHONPATH

One way to get around this is to set PYTHONPATH before starting python. This is just an environment variable with which you can tell python where to look for your modules.

eg.

export PYTHONPATH=/path/to/your/root/folder
python test_class/second_class.py

Using the -m switch for python

Be default python treats the directory of the main module as the place to look for other modules. However, if you use -m python will fall back to looking in the current directory. But you also need to specify the full name of the module you want to run (rather than as a file). eg.

python -m test_class.second_class

Writing a root entry point

In this we just define your main module at the base level (the directory that contains test_class. Python will treat this folder as the place to look for user modules and will find everything appropriately. eg.

main.py (in /path/to/your/root/folder)

from test_class.second_class import SecondClass

if __name__ == '__main__':
    print(SecondClass().minus_number())

at the command line

python main.py
Dunes
  • 37,291
  • 7
  • 81
  • 97
0

You could try importing the full files instead of using from file import class. Then you would only need to add the file name before referencing something from another file.