Importing a Python module from another module at the same level
I am working on a Python package with the basic structure given below and examples of what each Python file contains in curly braces.
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
This hasn't got me in trouble to date. However, I recently added a new module that does not import correctly (module3). I found several knitted strands, but none of these special tastes.
I can successfully install this using setup.py and then from the command line (any directory) import the following importers:
python
>>> from main_package.package1.module3.script3 import Class3A, Class3B
But when I copy the same line in script2 and reload the setup.py file (no errors), I get the following error on startup executable_script
:
ImportError: cannot import name Class3A
And then I also get the same error when I try to import into python via command line. BUT, I have this problem when I try to import Class3A from script3 to script2, not importing similar classes from script1 to script2. The only difference I can see is that script3 is a newer module than script1. Is there something that cannot be updated so that I can import this new module / class into an older script? Or is there something wrong with the structure that should have caught up with me?
Another detail is that this error only occurs when this line is added to another module at the same level, but the error occurs executable_script.py
in package2. For example. first line of error message
/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 document line. I am using Python 2.7.
source to share