我正在编写一个python模块。在Python2中一切正常,但在Python3中,导入失败。
这是我的代码结构。neuralnet/
__init__.py
train.py # A wrapper to train (does not define new things)
neuralnet.py # Defines the workhorse class neuralnet
layers/
__init__.py
inlayer.py # Defines input layer class
hiddenlayer.py
application/ # A seperate application (not part of the package)
classify.py # Imports the neuralnet class from neuralnet.py
train.py需要导入neuralnet.py的neuralnet类。
neuralnet.py需要导入layers/inlayer.py等
(我更喜欢相对进口。)
我有一个不同的应用程序(classify.py)需要导入这个模块。
我在哪里。。。from neuralnet.neuralnet import neuralnet
我试过几种进口方式。
或者我得到了一个错误(大部分是神秘的,比如父元素没有被导入)
1)运行train.py时(它是neuralnet模块的一部分)from . import layer # In file neuralnet.py
SystemError: Parent module '' not loaded, cannot perform relative import
或者
2)运行classify.py时(在模块外部)。from layer.inlayers import input_layer # In file neuralnet.py
ImportError: No module named 'layer'
我的进口货在Python2中很好地运了很多年。我想知道Python对我有什么期望?我是否应该将train.py移到模块外部(技术上它不是模块的一部分)?请提出最佳做法。
谢谢
拉凯什