Python 循环导入:包和模块之间的混淆
这是我的名为neuropredict的包(一组模块和一个init文件;其中一个模块也称为neuropredict): repo可在上获得,包的init文件(其文件夹内容如上所示)为: rhst.py中的导入代码(前20行)如下所示:Python 循环导入:包和模块之间的混淆,python,python-3.x,import,circular-dependency,Python,Python 3.x,Import,Circular Dependency,这是我的名为neuropredict的包(一组模块和一个init文件;其中一个模块也称为neuropredict): repo可在上获得,包的init文件(其文件夹内容如上所示)为: rhst.py中的导入代码(前20行)如下所示: $ 09:50:40 miner neuropredict >> head -n 20 neuropredict/rhst.py from __future__ import print_function __all__ = ['run', 'loa
$ 09:50:40 miner neuropredict >> head -n 20 neuropredict/rhst.py
from __future__ import print_function
__all__ = ['run', 'load_results', 'save_results']
if version_info.major==2 and version_info.minor==7:
import config_neuropredict as cfg
elif version_info.major > 2:
from neuropredict import config_neuropredict as cfg
else:
raise NotImplementedError('neuropredict supports only 2.7 or Python 3+. Upgrade to Python 3+ is recommended.')
包中的所有模块都需要导入的配置文件包含一组变量(如果_uname__==_umain__; code,则不包含任何)
对于Python2.7,以下测试脚本有效(仅导入所示代码的一部分)。然而,当我在Python3.6下运行时,一切都中断了(某种循环导入舞蹈)
从所显示的错误中,我怀疑混淆的地方是neuropredict(包)和neuropredict(模块),这不应该出现在Python 3+中,因为它强烈建议只使用显式导入
另外,test_rhst.py在pytest下运行时不会报告任何错误,但只有在终端上使用python3.6 test_rhst.py
运行时才会报告
错误消息没有很好的帮助(通常是无法导入名称rhst
或无法导入名称配置(u eunopredict
)),也没有告诉我原因,例如ModuleNotFoundError
此时,我正在考虑将模块重命名为与包名不同的名称,例如neupredict,或者压缩所有与python 2.7相关的代码,并专门在python 3+中工作(我不确定这是否可行)。因为没有我无法正确测试的最小工作示例,但是如果我正确理解您的设置,我认为问题在于您的test\u rhst.py
位于eunopredict/
文件夹中
当test_rhst.py
要求Python加载eunopredict
时,Python将在test_rhst.py
的目录中查找您的eunopredict.py
文件-它将找不到包含的目录
因此,如果您想要导入rhst.py
,您应该只导入rhst
。。。除非您可能希望测试您的软件包的工作方式与最终用户使用它的方式相同(您希望从neuropredict import rhst获得)。要做到这一点,您应该将测试文件放在包之外
为什么来自神经预测的导入rhst
有效?
因为neuropredict.py
本身不导入rhst
。因此,通过eunopredict.py
导入rhst.py
,而不是通过eunopredict/
程序包/目录导入
$ 09:50:27 miner neuropredict >> cat neuropredict/__init__.py
__all__ = ['neuropredict', 'rhst', 'visualize', 'freesurfer',
'config_neuropredict', 'model_comparison']
from sys import version_info
if version_info.major==2 and version_info.minor==7:
import neuropredict, config_neuropredict, rhst, visualize, freesurfer, model_comparison
elif version_info.major > 2:
from neuropredict import neuropredict, config_neuropredict, rhst, visualize, freesurfer, model_comparison
else:
raise NotImplementedError('neuropredict supports only 2.7 or Python 3+. Upgrade to Python 3+ is recommended.')
$ 09:50:40 miner neuropredict >> head -n 20 neuropredict/rhst.py
from __future__ import print_function
__all__ = ['run', 'load_results', 'save_results']
if version_info.major==2 and version_info.minor==7:
import config_neuropredict as cfg
elif version_info.major > 2:
from neuropredict import config_neuropredict as cfg
else:
raise NotImplementedError('neuropredict supports only 2.7 or Python 3+. Upgrade to Python 3+ is recommended.')
$ 09:51:22 miner neuropredict >> head -n 20 neuropredict/config_neuropredict.py
import matplotlib.pyplot as plt
NUM_TREES = 100
COMMON_FIG_SIZE = [9, 9]
CMAP_DATASETS = 'Paired'
$ 09:51:52 miner neuropredict >> head -n 20 neuropredict/test_rhst.py
import numpy as np
import os
import sys
from sys import version_info
from os.path import join as pjoin, exists as pexists, realpath
sys.dont_write_bytecode = True
from pyradigm import MLDataset
if version_info.major==2 and version_info.minor==7:
from neuropredict import rhst
elif version_info.major > 2:
from neuropredict.neuropredict import rhst
else:
raise NotImplementedError('neuropredict supports only 2.7 or Python 3+. Upgrade to Python 3+ is recommended.')