我有没有shebang的python脚本(它的问题)。当我用相对路径运行它时一切正常,异常直接到终端:
$ python -V
Python 2.6.6
$ cat 1.py
import os
print os.getcwd()
a=b
print c
$ python ./1.py
/home/user
Traceback (most recent call last):
File "./1.py", line 5, in <module>
a=b
NameError: name 'b' is not defined
$
但是只有当类型Control-C指定脚本的绝对路径时才会引发异常(注意跟踪部分之前的代码片段中的^ C符号):
$ python /home/user/1.py
/home/user
^CTraceback (most recent call last):
File "/home/v-yyushyn/1.py", line 5, in <module>
a=b
NameError: name 'b' is not defined
$
首先,我认为它的行为取决于绝对路径&#39;在子shell中运行,但经过短暂的调查后,我发现两个启动都在同一个shell中运行。
是的,可以放置一个shebang并运行脚本而不指定解释器。但有趣的是,在脚本的绝对/相对路径的情况下行为是不同的。
有人建议吗? 谢谢
更新:python -v /home/user/1.py
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/lib64/python2.6/site.pyc matches /usr/lib64/python2.6/site.py
import site # precompiled from /usr/lib64/python2.6/site.pyc
# /usr/lib64/python2.6/os.pyc matches /usr/lib64/python2.6/os.py
import os # precompiled from /usr/lib64/python2.6/os.pyc
import errno # builtin
import posix # builtin
# /usr/lib64/python2.6/posixpath.pyc matches /usr/lib64/python2.6/posixpath.py
import posixpath # precompiled from /usr/lib64/python2.6/posixpath.pyc
# /usr/lib64/python2.6/stat.pyc matches /usr/lib64/python2.6/stat.py
import stat # precompiled from /usr/lib64/python2.6/stat.pyc
# /usr/lib64/python2.6/genericpath.pyc matches /usr/lib64/python2.6/genericpath.py
import genericpath # precompiled from /usr/lib64/python2.6/genericpath.pyc
# /usr/lib64/python2.6/warnings.pyc matches /usr/lib64/python2.6/warnings.py
import warnings # precompiled from /usr/lib64/python2.6/warnings.pyc
# /usr/lib64/python2.6/linecache.pyc matches /usr/lib64/python2.6/linecache.py
import linecache # precompiled from /usr/lib64/python2.6/linecache.pyc
# /usr/lib64/python2.6/types.pyc matches /usr/lib64/python2.6/types.py
import types # precompiled from /usr/lib64/python2.6/types.pyc
# /usr/lib64/python2.6/UserDict.pyc matches /usr/lib64/python2.6/UserDict.py
import UserDict # precompiled from /usr/lib64/python2.6/UserDict.pyc
# /usr/lib64/python2.6/_abcoll.pyc matches /usr/lib64/python2.6/_abcoll.py
import _abcoll # precompiled from /usr/lib64/python2.6/_abcoll.pyc
# /usr/lib64/python2.6/abc.pyc matches /usr/lib64/python2.6/abc.py
import abc # precompiled from /usr/lib64/python2.6/abc.pyc
# /usr/lib64/python2.6/copy_reg.pyc matches /usr/lib64/python2.6/copy_reg.py
import copy_reg # precompiled from /usr/lib64/python2.6/copy_reg.pyc
# /usr/lib/python2.6/site-packages/abrt_exception_handler.pyc matches /usr/lib/python2.6/site-packages/abrt_exception_handler.py
import abrt_exception_handler # precompiled from /usr/lib/python2.6/site-packages/abrt_exception_handler.pyc
import encodings # directory /usr/lib64/python2.6/encodings
# /usr/lib64/python2.6/encodings/__init__.pyc matches /usr/lib64/python2.6/encodings/__init__.py
import encodings # precompiled from /usr/lib64/python2.6/encodings/__init__.pyc
# /usr/lib64/python2.6/codecs.pyc matches /usr/lib64/python2.6/codecs.py
import codecs # precompiled from /usr/lib64/python2.6/codecs.pyc
import _codecs # builtin
# /usr/lib64/python2.6/encodings/aliases.pyc matches /usr/lib64/python2.6/encodings/aliases.py
import encodings.aliases # precompiled from /usr/lib64/python2.6/encodings/aliases.pyc
# /usr/lib64/python2.6/encodings/utf_8.pyc matches /usr/lib64/python2.6/encodings/utf_8.py
import encodings.utf_8 # precompiled from /usr/lib64/python2.6/encodings/utf_8.pyc
Python 2.6.6 (r266:84292, Sep 11 2012, 08:34:23)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
/home/user
/usr/lib64/python2.6/os.pyc
/home/user/1.py
False
<function <lambda> at 0x7fa22e784c08>
dlopen("/usr/lib64/python2.6/lib-dynload/syslog.so", 2);
import syslog # dynamically loaded from /usr/lib64/python2.6/lib-dynload/syslog.so
# /usr/lib64/python2.6/traceback.pyc matches /usr/lib64/python2.6/traceback.py
import traceback # precompiled from /usr/lib64/python2.6/traceback.pyc
# /usr/lib64/python2.6/socket.pyc matches /usr/lib64/python2.6/socket.py
import socket # precompiled from /usr/lib64/python2.6/socket.pyc
dlopen("/usr/lib64/python2.6/lib-dynload/_socketmodule.so", 2);
import _socket # dynamically loaded from /usr/lib64/python2.6/lib-dynload/_socketmodule.so
dlopen("/usr/lib64/python2.6/lib-dynload/_ssl.so", 2);
import _ssl # dynamically loaded from /usr/lib64/python2.6/lib-dynload/_ssl.so
dlopen("/usr/lib64/python2.6/lib-dynload/cStringIO.so", 2);
import cStringIO # dynamically loaded from /usr/lib64/python2.6/lib-dynload/cStringIO.so
^CTraceback (most recent call last):
File "/home/user/1.py", line 10, in <module>
a=b
NameError: name 'b' is not defined
# clear __builtin__._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.exitfunc
# clear sys.exc_type
# clear sys.exc_value
# clear sys.exc_traceback
# clear sys.last_type
# clear sys.last_value
# clear sys.last_traceback
# clear sys.path_hooks
# clear sys.path_importer_cache
# clear sys.meta_path
# clear sys.flags
# clear sys.float_info
# restore sys.stdin
# restore sys.stdout
# restore sys.stderr
# cleanup __main__
# cleanup[1] cStringIO
# cleanup[1] encodings
# cleanup[1] site
# cleanup[1] syslog
# cleanup[1] abc
# cleanup[1] _codecs
# cleanup[1] _warnings
# cleanup[1] zipimport
# cleanup[1] encodings.utf_8
# cleanup[1] codecs
# cleanup[1] socket
# cleanup[1] signal
# cleanup[1] traceback
# cleanup[1] posix
# cleanup[1] encodings.aliases
# cleanup[1] exceptions
# cleanup[1] abrt_exception_handler
# cleanup[1] _ssl
# cleanup[1] _socket
# cleanup[2] copy_reg
# cleanup[2] posixpath
# cleanup[2] errno
# cleanup[2] _abcoll
# cleanup[2] types
# cleanup[2] genericpath
# cleanup[2] stat
# cleanup[2] warnings
# cleanup[2] UserDict
# cleanup[2] os.path
# cleanup[2] linecache
# cleanup[2] os
# cleanup sys
# cleanup __builtin__
# cleanup ints: 19 unfreed ints
# cleanup floats
答案 0 :(得分:3)
客户端已安装abrt
automatic bug reporting tool。它拦截异常并将它们发送到专用进程来跟踪崩溃。它是CentOS和Redhat系统上的常见插件(另请参阅Redhat documentation)
该进程挂起,并要求您使用中断来绕过它。
见abrt
configuration documentation;内省它正在做什么并将其调整为 not hang 。 abrt
FAQ也可能有所帮助;例如,那里列出了用于检查服务状态的命令。
我可以从hook abrt
installs看到具有相对路径的脚本被忽略;它只处理具有绝对路径的脚本中的异常(所以/home/user/1.py
),这就是为什么你只看到这个问题。
从相同的代码中我们可以看到它会将消息记录到syslog
;使用默认值,以便它们最终以/var/log/user.log
结尾;注意那里的脚本文件名(1.py
)。
很可能与abrtd
守护程序的套接字连接已挂起。套接字挂起的原因可能是多种原因including faulty network firmware。