3
我在与main()
脚本(getdata.py
)相同的目录中有vm.py
。在getdata.py
,我有Python模块(分配之前引用的模块名)
import vm
...
x = vm.Something()
那么Python抱怨
UnboundLocalError: local variable 'vm' referenced before assignment
这是为什么?导入时没有错误。
UPDATE
我发现,如果我做了
from vm import *
相反,它的工作。对于我制作的另一个文件/模块,也可以使用简单的import
。我将完整的代码上传到GitHub Gist https://gist.github.com/2259298
就其本身而言,应该工作。在你的代码中可能还有其他对vm的引用,因为你不认为它们是相关的,但实际上它们是。看到,例如,[这个问题](http://stackoverflow.com/questions/1188944/reason-for-unintuitive-unboundlocalerror-behaviour),或[this one](http://stackoverflow.com/questions/404534/蟒蛇-全局本地人和-unboundlocalerror)。 – DSM 2012-03-31 04:10:16
@DSM,更新后的观察和完整的代码上的要点(https://gist.github.com/2259298) – 2012-03-31 04:34:35
命令行参数永远不会在驼峰案例中完成; '--numReferences'通常以'--num-references'或'--references'完成。 – 2012-03-31 04:46:45