I thought I understand python import system, but it looks like I was wrong.
For some reason, I cannot use modules from my package unless I explicitly import them in the __init__.py
or in the script code. This is not what the documentation says and I have other projects (e.g., this one) that don't have this kind of issue.
To illustrate it, I created a super simple example. A project with a following structure:
sample_project
├── sample_project
│ ├── __init__.py
│ └── data.py
└── setup.py
setup.py
looks like this
import setuptools
setuptools.setup(name='sample_project', packages=setuptools.find_packages())
and data.py
like this
class Data:
pass
__init__.py
is empty.
After pip install -e .
I can import sample_project
, but sample_project.data
throws
AttributeError: module 'sample_project' has no attribute 'data'
Note that data.py
is not inside the submodule, so the issue is different from this one.
This happens either if I am in the project directory or outside it.
I can fix it via import sample_project.data
either in my script or in the __init__.py
, but I don't understand why it does not work. It feels like the default __all__
does not work for some reason.
In the project I mentioned above I have multiple python files inside new_semantic_parsing
directory. Some of them are imported in the __init__.py
, some are not, but I don't have this problem with either of them.
E.g.
import new_semantic_parsing
new_semantic_parsing.utils
works fine even though utils
is not explicitly imported in the __init__.py
.
Clearly, there is an issue somewhere, but I can't see it.