我想用这种结构来组织一个包裹:
/program.py
/__init__.py
/data/
/data/__init__.py
/data/method_1.py
/data/method_2.py
/data/classes.py
程序导入method_1和method_2,method_1和method_2导入类。但是我得到一个错误:ModuleNotFoundError:没有名为“ method_1”的模块。我应该如何组织此软件包以及应该在 init .py文件中写什么?
program.py:
import __init__
from data import classes
from data import method_1
from data import method_2
...
__init__.py:
__all__ = ['data']
from data import *
/data/__init__.py:
__all__ = ['classes', 'method_1', 'method_2']
from method_1 import *
from method_2 import *
from classes import *
/data/method_1.py: (and also /data/method_2.py)
import classes
...
答案 0 :(得分:0)
method_1
,method_2
和classes
are all in the data
package,因此:
# data/__init__.py
from .method_1 import *
from .method_2 import *
from .classes import *
...
# data/method_1.py
from . import classes
答案 1 :(得分:0)
由于data
软件包与program
位于同一目录中,因此应该可以:
#program.py
from .data import classes
from .data import method_1
from .data import method_2
# data/__init__.py
from .method_1 import *
from .method_2 import *
from .classes import *
#data/method_1.py
from . import classes
#data/method_2.py
from . import classes