Python文件运行,但找不到。呃?

时间:2015-01-13 12:03:04

标签: python

症状:显然正在使用文件:

File "/usr/lib/python2.7/dist-packages/salt/renderers/pyobjects.py", line 449, in render
  exec final_template in _globals, _locals
File "<string>", line 6, in <module>
File "/usr/lib/python2.7/dist-packages/salt/utils/pyobjects.py", line 293, in __getattr__
  raise AttributeError

大。但是,当我去/usr/lib/python2.7/dist-packages/salt/utils/pyobjects.py时,它不在那个目录中,并且它的pyc都没有:

ls /usr/lib/python2.7/dist-packages/salt/utils/py*
/usr/lib/python2.7/dist-packages/salt/utils/pydsl.py  /usr/lib/python2.7/dist-packages/salt/utils/pydsl.pyc

为什么会这样?我对此缺少什么 - 我尝试过搜索,但没有找到任何有用的东西。

如果有任何用途,请更新:

$ python -v
>>> import salt.utils.pyobjects
# /usr/local/lib/python2.7/dist-packages/salt/utils/pyobjects.pyc matches /usr/local/lib/python2.7/dist-packages/salt/utils/pyobjects.py
import salt.utils.pyobjects # precompiled from /usr/local/lib/python2.7/dist-packages/salt/utils/pyobjects.pyc

1 个答案:

答案 0 :(得分:1)

您发布的python -v详细输出显示该文件是从其他位置导入的:

/usr/local/lib/python2.7/dist-packages/salt/utils/pyobjects.pyc
#    ^^^^^

但你正在寻找:

ls /usr/lib/python2.7/dist-packages/salt/utils/py*
#      ^^^^

.pyc个文件在字节码中存储文件名,文件必须已从/usr/lib移至/usr/local/lib。因此,当显示回溯时,字节码中的文件名仍然指向旧位置。

您可以强制重新编译(使用sudo或其他方式运行以获得写访问权限):

python -m compileall -f /usr/local/lib/python2.7/dist-packages/salt

生成存储新位置的.pyc个文件。