Skip to main content
 首页 » 编程设计

Python 导入错误

2025年05月04日10lovecherry

$ bin/python src/app/utils/loader.py newTasks checkForStuff  
Traceback (most recent call last): 
  File "bin/python", line 135, in <module> 
    execfile(__file__) 
  File "src/app/utils/loader.py", line 154, in <module> 
    module = __import__(args.module) 
ImportError: No module named newTasks 

来自以下 loader.py:

if __name__ == '__main__': 
    parser = argparse.ArgumentParser(description='Runs python') 
    parser.add_argument('module', metavar='module', help="the target module") 
    args = parser.parse_args() 
    module = __import__(args.module) 

newTasks.py 与 loader.py 在同一目录中。它在我的开发箱上工作,我做了一个 svn 导出到暂存,上面发生了。有什么指点吗?

我唯一能解决的实质性区别是这次我用完了 virtualenv。

编辑: 我将命令行更改为:

$ bin/python src/app/utils/loader.py utils.newTasks checkForStuff  

并且发现模块正常。但我的下一个命令是调用模块函数之一 checkForStuff:

parser.add_argument('func',metavar='function', help="the target function name") 
... 
func_param = getattr(module,args.func) 

现在找不到函数了?

Traceback (most recent call last): 
  File "bin/python", line 135, in <module> 
    execfile(__file__) 
  File "src/app/utils/loader.py", line 156, in <module> 
    func_param = getattr(module,args.func) 
AttributeError: 'module' object has no attribute 'checkForStuff' 

如果我进入 bin/python:

>>> import utils.newTasks as nt 
>>> nt.checkForStuff 
<function checkForStuff at 0x29b2f50> 

这很烦人!

请您参考如下方法:

bin/python 不是您暂存计算机上的 python 二进制文件。检查它是否像普通python一样将脚本目录插入到sys.path中。