Python 3.x Python3:module';x';没有属性';y';

Python 3.x Python3:module';x';没有属性';y';,python-3.x,python-3.7,Python 3.x,Python 3.7,Python导入语句把我搞糊涂了。有人能帮我弄清楚吗 文件树如下所示 root +- notebook.ipynb +- lib/ +- basestation_config.py +- config.py +- config/ +- valence_pod.json +- etc… 在config.py中,我有: import json import os default_config_path = os.path.abspath(os.path.join(os.path.

Python导入语句把我搞糊涂了。有人能帮我弄清楚吗

文件树如下所示

root
+- notebook.ipynb
+- lib/
  +- basestation_config.py
  +- config.py
+- config/
  +- valence_pod.json
  +- etc…
config.py
中,我有:

import json
import os

default_config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'config'))

def read_filepath(filepath):
    with open(filepath, "r") as read_file:
        return json.load(read_file)

def read(filename):
    filepath = os.path.join(default_config_path, filename) + '.json'
    return read_filepath(filepath)
import config as config
# … a buncha class libraries, including BasestationConfig
def read_basestation_config(config_name = 'valence_pod'):
    return BasestationConfig(config.read(config_name))
basestation\u config.py
中,我有:

import json
import os

default_config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'config'))

def read_filepath(filepath):
    with open(filepath, "r") as read_file:
        return json.load(read_file)

def read(filename):
    filepath = os.path.join(default_config_path, filename) + '.json'
    return read_filepath(filepath)
import config as config
# … a buncha class libraries, including BasestationConfig
def read_basestation_config(config_name = 'valence_pod'):
    return BasestationConfig(config.read(config_name))
notebook.ipynb
中,我有一个测试单元:

import lib.basestation_config as bsc
bs_config = bsc.read_basestation_config()
display(bs_config)
当我运行它时,我得到:

<module 'config' (namespace)>
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-f2942fb5fb2d> in <module>
      1 import lib.basestation_config as bsc
----> 2 bs_config = bsc.read_basestation_config()
      3 display(bs_config)

/mnt/eng_store/pod/logs/embedded/utils/logutils/indot-py/lib/basestation_config.py in read_basestation_config(config_name)
    270 def read_basestation_config(config_name = 'valence_pod'):
    271     print(config)
--> 272     return BasestationConfig(config.read(config_name))

AttributeError: module 'config' has no attribute ‘read’

---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
1导入lib.basestation\u配置为bsc
---->2 bs_config=bsc.read_基站_config()
3显示(bs_配置)
/读取基站配置(配置名称)中的mnt/eng\u store/pod/logs/embedded/utils/logutils/indot py/lib/basestation\u config.py
270 def read_基站配置(配置名称='valence_pod'):
271打印(配置)
-->272返回BasestationConfig(config.read(config_name))
AttributeError:模块“config”没有属性“read”

当您导入配置时,Python使用的是配置文件夹(带有JSON文件的文件夹),而不是
lib/config.py
模块。导入后,您可以通过打印
config.\uuuuu路径\uuuuu
来检查这一点:

import config as config
print(config.__path__)
# _NamespacePath(['/path/to/root/config']) 
\u NamespacePath
表示文件夹配置被视为,它不包含类似于的
\uuuuu init\uuuuuuuuupy
,但名称与导入的“配置”匹配

在查找名为“foo”的模块或包时,为每个目录 在父路径中:

  • 如果找到
    /foo/\uuuuu init\uuuuu.py
    ,则导入并返回一个常规包
  • 如果没有,但是找到了
    /foo.{py,pyc,so,pyd}
    ,则导入并返回一个模块。扩展的确切列表因平台和是否指定了-O标志而有所不同。这里的清单很有代表性
  • 如果未找到,但找到了
    /foo
    ,并且是一个目录,则会记录该目录,并继续扫描父路径中的下一个目录
  • 否则,将继续扫描父路径中的下一个目录
由于
/config
导入配置
目标匹配,您的设置属于项目符号3。您可能会想知道这里是什么?这取决于存储在
sys.path
中的,它是Python将在其中查找导入目标的所有目录的列表。在根目录下运行测试脚本时,根目录将添加到
sys.path

包含正在运行的脚本的目录位于搜索路径的开头,位于标准库路径的前面

通过在基站的import config之前添加以下内容来检查:

这就解释了原因。要解决此问题,可以执行以下操作:

  • 将config文件夹重命名为其他文件夹(例如jsonfiles),以防止将来出现类似的错误,并将其与config.py模块区分开来
  • 通过在lib下添加一个
    \uu init\uu.py
    文件,将lib更改为遵循,以清楚地将其标记为包

    lib
    ├── __init__.py
    ├── basestation_config.py
    └── config.py
    
  • 最后,在basestation_config.py中明确说明您正在同一目录中导入config.py。
    from . import config as config    
    print(config.__file__) 
    # /path/to/lib/config.py
    
  • 请注意,在执行步骤3之前,如果您在前面添加了
    打印(config.\uuuu path\uuuu)
    请确保在应用更正的代码后将其删除,因为它很可能在您的config.py上不可用(您可能会得到“AttributeError:module'lib.config'没有属性
    \uu path\uuu


    这些更改之后,它现在应该可以工作。

    消息告诉您,您导入的“配置”模块没有名为“读取”的方法。