我正在写一个python模块neuralnet。它在Python2中工作得很好,但在Python3中,导入失败。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需要导入层/ 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中工作得非常好。我想知道Python3对我的期望是什么?我应该将train.py移到我的模块外面(技术上它不是模块的一部分)?请建议最佳做法。
感谢 拉克什
按'net.py'你的意思是'neuralnet.py'?您的示例不包含名为'net.py'的文件。另外,如果train.py不是你的软件包的一部分,那么是的,你应该将它移出。 – BrenBarn 2015-02-08 23:46:58