如何从python中的不同目录导入函数

时间:2014-04-23 11:27:34

标签: python python-2.7 import module

我有以下文件结构:

/usr/share/pyshared/orca/scripts/toolkits/Gecko/script.py

在此文件中 def getExtents 是我必须在以下文件中访问的功能,即: /usr/share/pyshared/orca/scripts/default.py

所以我试图将script.py中的函数导入到以下文件中:

/usr/share/pyshared/orca/scripts/default.py

/usr/share/pyshared/orca/scripts/default.py中添加了以下行:

from orca.scripts.toolkits.Gecko.script import getExtents

但我得到以下错误:

 File "<string>", line 1, in <module>
  File "/usr/lib/pymodules/python2.7/orca/orca.py", line 1498, in main
    init(pyatspi.Registry)
  File "/usr/lib/pymodules/python2.7/orca/orca.py", line 1215, in init
    loadUserSettings()
  File "/usr/lib/pymodules/python2.7/orca/orca.py", line 837, in loadUserSettings
    _scriptManager.activate()
  File "/usr/lib/pymodules/python2.7/orca/script_manager.py", line 80, in activate
    self.setActiveScript(self.getScript(None), "activate")
  File "/usr/lib/pymodules/python2.7/orca/script_manager.py", line 247, in getScript
    appScript = self.getDefaultScript()
  File "/usr/lib/pymodules/python2.7/orca/script_manager.py", line 214, in getDefaultScript
    import scripts.default as default
  File "/usr/lib/pymodules/python2.7/orca/scripts/default.py", line 57, in <module>
    from orca.scripts.toolkits.Gecko.script import getExtents
  File "/usr/lib/pymodules/python2.7/orca/scripts/toolkits/Gecko/__init__.py", line 1, in <module>
    from script import Script
  File "/usr/lib/pymodules/python2.7/orca/scripts/toolkits/Gecko/script.py", line 52, in <module>
    import orca.scripts.default as default
AttributeError: 'module' object has no attribute 'default'

1 个答案:

答案 0 :(得分:0)

我想你可能需要

from orca.scripts import default

此外,正如评论中所述,请检查您是否已获得相关__init__.py

编辑:重新阅读堆栈跟踪,看起来你也有一个循环导入,你正试图在&#52行中导入orca.scripts.default 34; /usr/lib/pymodules/python2.7/orca/scripts/toolkits/Gecko/script.py"