我正在編寫一個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移到模塊外部(技術上它不是模塊的一部分)?請提出最佳做法。
謝謝
拉凱什