处理python脚本的绝对/相对路径时的异常处理

处理python脚本的绝对/相对路径时的异常处理,python,linux,exception-handling,Python,Linux,Exception Handling,我的python脚本没有shebang(这是问题)。当我使用相对路径运行它时,一切正常,异常直接引发到终端: $ 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 Nam

我的python脚本没有shebang(这是问题)。当我使用相对路径运行它时,一切正常,异常直接引发到终端:

$ 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
$
$python-V
Python 2.6.6
$cat 1.py
导入操作系统
打印os.getcwd()
a=b
打印c
$python./1.py
/家庭/用户
回溯(最近一次呼叫最后一次):
文件“/1.py”,第5行,在
a=b
名称错误:未定义名称“b”
$
但只有在指定脚本的绝对路径(请注意回溯部分之前的代码段中的^C符号)时,键入Control-C才会引发异常:

$python/home/user/1.py
/家庭/用户
^CTraceback(最近一次通话最后一次):
文件“/home/v-yyushyn/1.py”,第5行,在
a=b
名称错误:未定义名称“b”
$
首先,我认为它的行为取决于在子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
#安装zipimport挂钩
进口zipimport#内置
#已安装的zipimport挂钩
#/usr/lib64/python2.6/site.pyc matches/usr/lib64/python2.6/site.py
导入站点#从/usr/lib64/python2.6/site.pyc预编译
#/usr/lib64/python2.6/os.pyc matches/usr/lib64/python2.6/os.py
从/usr/lib64/python2.6/os.pyc导入预先编译的os
导入错误号#内置
导入posix#内置
#/usr/lib64/python2.6/posixpath.pyc匹配/usr/lib64/python2.6/posixpath.py
导入posixpath#从/usr/lib64/python2.6/posixpath.pyc预编译
#/usr/lib64/python2.6/stat.pyc matches/usr/lib64/python2.6/stat.py
导入stat#从/usr/lib64/python2.6/stat.pyc预编译
#/usr/lib64/python2.6/genericpath.pyc匹配/usr/lib64/python2.6/genericpath.py
导入genericpath#从/usr/lib64/python2.6/genericpath.pyc预编译
#/usr/lib64/python2.6/warnings.pyc匹配/usr/lib64/python2.6/warnings.py
导入警告#从/usr/lib64/python2.6/warnings.pyc预编译
#/usr/lib64/python2.6/linecache.pyc matches/usr/lib64/python2.6/linecache.py
导入linecache#从/usr/lib64/python2.6/linecache.pyc预编译
#/usr/lib64/python2.6/types.pyc matches/usr/lib64/python2.6/types.py
导入类型#从/usr/lib64/python2.6/types.pyc预编译
#/usr/lib64/python2.6/UserDict.pyc匹配/usr/lib64/python2.6/UserDict.py
导入UserDict#从/usr/lib64/python2.6/UserDict.pyc预编译
#/usr/lib64/python2.6//u abcoll.pyc matches/usr/lib64/python2.6//u abcoll.py
从/usr/lib64/python2.6//u abcoll.pyc导入预编译的
#/usr/lib64/python2.6/abc.pyc matches/usr/lib64/python2.6/abc.py
导入abc#从/usr/lib64/python2.6/abc.pyc预编译
#/usr/lib64/python2.6/copy_reg.pyc matches/usr/lib64/python2.6/copy_reg.py
导入从/usr/lib64/python2.6/copy_reg.pyc预编译的副本
#/usr/lib/python2.6/site-packages/abrt_exception_handler.pyc匹配/usr/lib/python2.6/site-packages/abrt_exception_handler.py
导入从/usr/lib/python2.6/site-packages/abrt\u exception\u handler.pyc预编译的abrt\u异常处理程序
导入编码#目录/usr/lib64/python2.6/encodings
#/usr/lib64/python2.6/encodings/_init__.pyc matches/usr/lib64/python2.6/encodings/_init__.py
导入编码#从/usr/lib64/python2.6/encodings/_init__.pyc预编译
#/usr/lib64/python2.6/codecs.pyc matches/usr/lib64/python2.6/codecs.py
导入编解码器#从/usr/lib64/python2.6/codecs.pyc预编译
导入"编解码器"内置
#/usr/lib64/python2.6/encodings/aliases.pyc matches/usr/lib64/python2.6/encodings/aliases.py
导入encodings.alias#从/usr/lib64/python2.6/encodings/alias.pyc预编译
#/usr/lib64/python2.6/encodings/utf_8.pyc matches/usr/lib64/python2.6/encodings/utf_8.py
导入encodings.utf#u 8#从/usr/lib64/python2.6/encodings/utf#u 8.pyc预编译
Python2.6.6(r266:84292120912年9月11日08:34:23)
[GCC 4.4.6 20120305(Red Hat 4.4.6-4)]关于linux2
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
/家庭/用户
/usr/lib64/python2.6/os.pyc
/主页/用户/1.py
假的
dlopen(“/usr/lib64/python2.6/lib dynload/syslog.so”,2);
导入syslog#从/usr/lib64/python2.6/lib-dynload/syslog.so动态加载
#/usr/lib64/python2.6/traceback.pyc matches/usr/lib64/python2.6/traceback.py
导入回溯#从/usr/lib64/python2.6/traceback.pyc预编译
#/usr/lib64/python2.6/socket.pyc匹配/usr/lib64/python2.6/socket.py
导入套接字#从/usr/lib64/python2.6/socket.pyc预编译
dlopen(“/usr/lib64/python2.6/lib dynload/_socketmodule.so”,2);
导入_socket#从/usr/lib64/python2.6/lib-dynload//u socketmodule.so动态加载
dlopen(“/usr/lib64/python2.6/lib dynload/_ssl.so”,2);
导入_ssl#从/usr/lib64/python2.6/lib-dynload//u ssl.so动态加载
dlopen(“/usr/lib64/python2.6/lib dynload/cStringIO.so”,2);
导入cStringIO#从/usr/lib64/python2.6/lib-dynload/cStringIO.so动态加载
^CTraceback(最近一次通话最后一次):
文件“/home/user/1.py”,第10行,在
a=b
名称错误:未定义名称“b”
#清除内置的_
#清除系统路径
#清除系统argv
#清除系统ps1
#清除sys.ps2
#清除sys.exitfunc
#清除系统exc_类型
#清除系统exc_值
#清除sys.exc\u回溯
#清除系统最后一个类型
#清除系统最后一个值
#清除系统上次追踪
#清除系统路径挂钩
#清除sys.path\u导入器\u缓存
#清除sys.meta_路径
#清除系统标志
#清除系统浮动信息
#还原sys.stdin
#还原系统stdout
#还原sys.stderr
#清理主管道__
#清理[1]cStringIO
#清理[1]编码
#清理[1]场地
#清理[1]系统日志
#清理[1]abc
#清理[1]\u编解码器
#清理[1]\u警告
#清理[1]zipimport
#清理[1]编码.utf_8
#清理[1]编解码器
#清理[1]套接字
#清除[1]信号
#清理[1]回溯
#清理[1]posix
#清理[1]enc
# 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