Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Python module from another module in the same level

I am working on a Python package with the basic structure listed below, and examples of what each Python file contains in curly brackets.

MAIN_PACKAGE/
    setup.py
    main_package/
        __init__.py 
        { 
        import package1
        import package2
        __all__=['main_package']
        }
        package1/
            __init__.py
            {
            import module1
            import module2
            __all__=['package1']
            }
            module1/
                __init__.py
                {
                from script1 import Class1A, Class1B
                __all__ = ['script1']
                }
                script1.py
                {contains 2 classes: Class1A and Class1B}

            module2/
                __init__.py
                {
                from script2 import Class2A, Class2B
                __all__ = ['script2']
                }
                script2.py
                {contains 2 classes: Class2A, Class2B}

            module3/
                __init__.py
                {
                from script3 import Class3A, Class3B
                __all__ = ['script3'] 
                }
                script3.py
                {contains 2 classes: Class3A, Class3B}

        package2/
                executable_script.py

It has not given me trouble to date. However, I have recently added a new module that does not import correctly (module3). I have found a few related threads, but none of this particular flavor.

I can successfully install this with setup.py, and then from the command line (any directory), the following imports correctly:

python
>>> from main_package.package1.module3.script3 import Class3A, Class3B

But, when I copy the same line into script2 and rerun the setup.py file (without error), I get the following error when I run the executable_script:

ImportError: cannot import name Class3A

And then I also get the same error when I try to import to python via the command line. BUT, I only have this problem when I try to import Class3A from script3 into script2, not for importing the analogous classes from script1 into script2. The only difference I can see is that script3 is a newer module than script1. Is there something that may not be updated in order for me to import this new module/class into an older script? Or is there something wrong with the structure that was bound to catch up to me?

Another detail is that this error occurs only when that line is added to another module at the same level, but the error originates in the executable_script.py in package2. E.g. the first line of the error message is

/usr/bin/executable_script, line 9, in <module>load_entry_point('MAIN_PACKAGE==0.1.0','console_scripts','executable_script')().

Line 9 of the executable script is part of the doc string. I am using Python 2.7.

like image 537
user3403779 Avatar asked Nov 09 '22 13:11

user3403779


1 Answers

Probably you are in a circular import. In ClassA import ClassB and in somewhere in ClassB import ClassA or with more files, like in ClassA import Class B, in ClassC import ClassB and ClassA.

Try using absolute import:

from .script3 import Class3A
like image 111
Gocht Avatar answered Nov 14 '22 21:11

Gocht