Reputation: 5364
I realize that questions like this have been asked thousands and thousands of times, but I cannot figure out how to successfully import my data
submodule.
Directory structure
┌ dummy
├── setup.py
└─┬ dummy
├── __init__.py
├── foo.py
└─┬ data
├── __init__.py
└── data_bar.py
My top-level __init__.py
contains
from .foo import *
from .data import *
and data/__init__.py
is empty.
I pip install the package to a fresh virtual environment
pip install /path/to/dummy
which works fine. Then in an ipython shell run from a completely directory, I try importing the package
H:\Desktop$ ipython
In [1]: import dummy
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-cfc16ef2ccc8> in <module>
----> 1 import dummy
C:\Users\rlane\AppData\Local\Continuum\miniconda3\envs\secdetect_test\lib\site-packages\dummy\__init__.py in <module>
1 from .foo import *
----> 2 from .data import *
ModuleNotFoundError: No module named 'dummy.data'
foo.py
imports without error, but after trying every conceivable variation of from .data import *
I cannot get the code in the data
submodule to load.
Variations within top-level __init__.py
from .data import *
from data import *
from . import data
from .data import data_bar
from .data.data_bar import *
All of which result in the same ModuleNotFoundError
.
Upvotes: 2
Views: 1581
Reputation: 5364
The problem is that dummy.data
is missing as a package in the setup.py
script.
setup(
name=DISTNAME,
author=MAINTAINER,
...
packages=[
'dummy',
'dummy.data'
],
...
long_description=open('README.md').read(),
)
With this configuration, from .data import *
, and from . import data
in the top-level __init__.py
both work.
Upvotes: 3
Reputation: 409
I have tried the same, but it worked for me.
This is my structure(note that all these files are in a folder called dummy).
Python files:
foo.py:
def foo_module():
return 'foo module'
data_bar.py:
def data_bar_module():
return 'Data bar'
test.py:
import dummy
#from dummy.data import data_bar
#print(dummy.foo.foo_module())
#print(data_bar.data_bar_module())
When I do python3 test.py
it gives me no errors and when I uncomment the commented lines in test.py
file it gives:
foo module
Data bar
I am not exactly sure why it does work, but try to import data package like this, it may work.
from dummy.data import *
Hope this was helpful.
Upvotes: 0