【问题标题】:How do I debug a "can not import" error on package import如何在包导入时调试“无法导入”错误
【发布时间】:2013-12-07 06:57:56
【问题描述】:

我是 python 新手,并试图通过this 教程了解 python fuse。我用 pip 安装了 pythonfuse。我通过下载 dmg 并在 os x 上安装来安装 os x fuse。当我从教程中运行这行代码from fuse import FUSE, FuseOSError, Operations 时,我看到了:

akh2103$ python myfuse.py 
Traceback (most recent call last):
  File "myfuse.py", line 10, in <module>
    from fuse import FUSE, FuseOSError, Operations
ImportError: cannot import name FUSE

好像找不到fuse包,找不到python fuse包或者包内找不到FUSE、FuseOSError和Operations方法。哪一个?当我输入import fuse 时,Python 去哪里寻找 fuse 包?我习惯于在 java 中对路径进行分类:是否有 python 等价物?我对python很陌生。我该如何开始调试。

【问题讨论】:

标签: python fuse


【解决方案1】:

它在 /Library/Python//site-packages 中查找。

您可能有多个版本,这可能是问题的原因。

找出 pip 安装保险丝的位置。

您可以使用 PYTHONPATH 环境变量来添加其他文件夹。

【讨论】:

    【解决方案2】:

    找到了保险丝模块(否则您会看到“没有名为保险丝的模块”)。您得到的错误意味着在模块保险丝中找不到“FUSE”符号。

    我的猜测是 FUSE 有几个 python 绑定,您可能正在查看与您正在加载的模块不同的模块的教程。另一种选择是不同版本之间库的一些剧烈变化。

    如果要查看模块导出的所有符号,请使用dir()

    import fuse
    dir(fuse)
    

    【讨论】:

      【解决方案3】:

      假设这是你的目录结构:

      myapp/
          firstpackage/
              __init__.py
              firstmodule.py
          secondpackage/
              __init__.py
              secondmodule.py
          __init__.py
          myfirstapp.py
      

      firstmodule.py

      def first_function(data):
          return data
      
      def second_function(data):
          return data
      

      现在假设我们正在使用 :mod:`myfirstapp`。

      如果我们想访问:func:`first_function`,我们会像这样导入:

      from myapp.firstpackage.firstmodule import first_function
      print first_function('foo')
      
      'firstpackage' 目录中的

      :mod:`__init__` 允许从其目录外部访问 :mod:`firstmodule`。在目录中包含 :file:`__init__.py` 会使该目录成为 Python 包。

      但是,最好像这样导入整个模块:

      import myapp.firstpackage.firstmodule as firstmodule
      print firstmodule.first_function('foo')
      print firstmodule.second_function('bar')
      

      另一种方法是:

      from myapp.firstpackage import firstmodule
      print firstmodule.second_function('foo')
      

      这样一来,您的模块中的所有内容都可以访问,并且可读性更好。

      话虽如此,您收到的 :exc:`ImportError` 是因为 :mod:`fuse` 中不存在 'FUSE',无论是数据、类还是函数。

      打开 fuse.py 并搜索“FUSE”并查看是否出现任何问题。寻找:

      def FUSE(): ...
      class FUSE(..): ...
      FUSE = ...
      

      我知道整个包/模块课程与您的问题无关,但您说您是新手,所以我想我会详细说明:P

      【讨论】:

        猜你喜欢
        • 2022-12-21
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        • 2021-07-26
        • 2021-01-18
        • 2018-07-20
        • 1970-01-01
        • 2022-06-17
        相关资源
        最近更新 更多