我具有以下文件结构
.
└── text_classifier
├── Experiment.py
├── __init__.py
├── constants.py
├── hyperparameters
│ ├── Hyperparameter.py
│ └── __init__.py
├── models
│ ├── __init__.py
│ ├── han.py
│ └── model.py
├── trainers
│ ├── __init__.py
│ ├── dataset_trainer.py
│ └── trainer.py
└── utils
├── __init__.py
├── default_conf.yml
├── tf_utils.py
└── utils.py
我想从Ipython控制台上import text_classifier
。
text_classifier/__init__.py
包含
import builtins
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
sys.path.append(os.path.dirname(__file__))
sys.path.append(__file__)
print('\n'.join(sys.path))
from .experiment import Experiment
from .hyperparameters import HP
try:
from IPython.lib import deepreload
builtins.reload = deepreload.reload
except:
pass
但是,在与import text_classifier as tcl
相同的位置运行text_classifier
会产生以下结果:
/Users/victor/.pyenv/versions/3.6.4/Python.framework/Versions/3.6/lib/python36.zip
/Users/victor/.pyenv/versions/3.6.4/Python.framework/Versions/3.6/lib/python3.6
/Users/victor/.pyenv/versions/3.6.4/lib/python3.6/lib-dynload
/Users/victor/.pyenv/versions/3.6.4/Python.framework/Versions/3.6/lib/python3.6/site-packages
/Users/victor/.pyenv/versions/3.6.4/lib/python3.6/site-packages
/Users/victor/.pyenv/versions/3.6.4/Python.framework/Versions/3.6/lib/python3.6/site-packages/IPython/extensions
/Users/victor/.ipython
/Users/victor/Documents/documentation
/Users/victor/Documents/documentation/han
/Users/victor/Documents/documentation/han/text_classifier
/Users/victor/Documents/documentation/han/text_classifier/__init__.py
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-256767c7aff3> in <module>()
----> 1 import text_classifier as tl
~/Documents/documentation/han/text_classifier/__init__.py in <module>()
10 print('\n'.join(sys.path))
11
---> 12 from .experiment import Experiment
13 from .hyperparameters import HP
14
ModuleNotFoundError: No module named 'text_classifier.experiment'
很显然,text_classifier
在路径(...documentation/han/text_classifier
)中。有想法吗?
答案 0 :(得分:2)
Experiment.py
模块使用大写字母E
,但是您导入的experiment
没有此模块
.
└── text_classifier
├── Experiment.py
...
我认为您需要这样导入
# text_classifier/__init__.py
# if you have `Experiment` class
from .Experiment import Experiment
# or simple import `Experiment` module
from . import Experiment