您不应将脚本命名为现有模块.特别是如果标准.
也就是说,您可以触摸sys.path来修改库加载顺序
~# cat getopt.py print "HI" ~# python Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> import getopt HI ~# python Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.path.remove('') >>> import getopt >>> dir(getopt) ['GetoptError', '__all__', '__builtins__', '__doc__', '__file__', '__name__', 'do_longs', 'do_shorts', 'error', 'getopt', 'gnu_getopt', 'long_has_args', 'os', 'short_has_arg']
此外,您可能希望避免完全导入并以不同方式执行,如下所示:
import sys sys.path.remove('') from getopt import getopt sys.path.insert(0,'') opts, args = getopt(sys.argv[1:], "h:s") for key,value in opts: print key, "=>", value