5

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.

1
  • from main_package.package1.module3 import Class3A, Class3B should work since you imported them in module3 __init__ you dont need the .script3 at the end Commented May 14, 2015 at 22:42

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this and still have the error. Within each script, I import the modules outside of the classes. So at the top of each script the modules are imported, but nothing is imported within a specific class.
OK you were right. script3 and script2 were trying to import each other. This was a straight forward fix for this package (script3 didn't need to import script2, but script2 needed to import script3). But I wonder if this is absolutely impossible. It seems like it may be desirable in some situations.
I had situations when I though that was needed, but there-s no way, If you can-t find other way to solve your need with just one file importing other (in just one way) you will have to use and intermediate file.

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.