关于Python中if __name__ == '__main__':的问题

bluishsky 2004-04-13 09:40:14
您好,请教一个很初级的python问题:
我在研究BitTorrent的时候自己写了一段代码,存入testMain.py这个文件:
#!/usr/bin/env python
# -*- coding: cp936 -*-

# Written by Bram Cohen
# see LICENSE.txt for license information

import sys
sys.path.append('D:\develop\Python\BitTorrent-3.3\BitTorrent')
sys.path.append('D:\develop\Python\BitTorrent-3.3')

from sys import argv
from BitTorrent.parseargs import parseargs, formatDefinitions

defaults = [
('piece_size_pow2', 18,
"which power of 2 to set the piece size to"),
('comment', '',
"optional human-readable comment to put in .torrent"),
('target', '',
"optional target file for the torrent")
]

if __name__ == '__main__':
if len(argv) < 3:
print 'usage is -'
print argv[0] + ' file trackerurl [params]'
print
print formatDefinitions(defaults, 80)
else:
try:
config, args = parseargs(argv[3:], defaults, 0, 0)
print 'config',config
print 'args',args
except ValueError, e:
print 'error: ' + str(e)
print 'run with no args for parameter explanations'

我觉得我的问题应该很初级,但是我确实没看到相关资料,只在Python编程宝典时看到了这样一段描述的话:
从if __name__ == '__main__':往下的代码是通用于许多GUI程序的一项约定。该行检测名称空间是否为
“__main__”,并在条件成立(即为文件调用了解释器)的前提执行后续语句。如果文件作为一个模块导入,
该条件就不成立,如果程序单独运行,而非作为模块在另一个程序中使用,后续语句就会执行。

我现在的问题是,如何才能使程序单独运行,或者如何才能使程序在shell下单独运行,并且输入我的程序参数。

我作了以下尝试:
>>> import testMain
>>> dir()
['__builtins__', '__doc__', '__name__', 'argv', 'defaults', 'formatDefinitions', 'parseargs', 'sys', 'testMain']
>>> testMain
<module 'testMain' from 'D:\PROGRA~1\Python\testMain.py'>
>>> testMain arg01 arg02
SyntaxError: invalid syntax
>>> testMain
<module 'testMain' from 'D:\PROGRA~1\Python\testMain.py'>
>>> testMain [arg01,arg02]

Traceback (most recent call last):
File "<pyshell#15>", line 1, in -toplevel-
testMain [arg01,arg02]
NameError: name 'arg01' is not defined
>>> testMain ['','']

Traceback (most recent call last):
File "<pyshell#16>", line 1, in -toplevel-
testMain ['','']
TypeError: unsubscriptable object
>>> testMain [myfile, http://myfile/announce]
SyntaxError: invalid syntax
>>> testMain ['myfile.txt', 'http://myfile/announce']

Traceback (most recent call last):
File "<pyshell#18>", line 1, in -toplevel-
testMain ['myfile.txt', 'http://myfile/announce']
TypeError: unsubscriptable object
>>>

都没能找到在命令行调用该模块的方式
请指教
...全文
1524 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
duracell 2004-04-14
  • 打赏
  • 举报
回复
在idle或这python里面无法是调用命令行参数的
要写到程序里面
然后
python testMain arg1 arg2 ....
duracell 2004-04-14
  • 打赏
  • 举报
回复
testMain ( ['',''] )

命令行:
程序里加:

import sys
if __name__='__main__'
if len(sys.argv)==....: 你自己参数的条件
testMain ( sys.argv[1:])
else:
比如出错显示
注意python argv 的第一项也就是sys.argv[0]是python程序的名称(全路径名)
如python testMain.py aa bb
那么sys.argv[0]='[path]/testMain.py' [path]是你的testMain.py的绝对路径
所以如果你的程序需要两个参数那么len(sys.argv)应该等于3才是对的而不像java需要的是2

bluishsky 2004-04-14
  • 打赏
  • 举报
回复
谢谢duracell()~~~!!!!

太爱你了!!!!

我在dos下一试就成功了

我马上动手学pyunit, thank u!!!!
duracell 2004-04-14
  • 打赏
  • 举报
回复
例外如果要测试的话
用pyunit更好
人工测试不太方便吧
duracell 2004-04-14
  • 打赏
  • 举报
回复
不是在idle里面
而是在dos窗口下面
如果一定要在idle里面测试
可以用模拟的方式拉
比如你要命令行这样 python testMain arg1 arg2
那么就在idle里面用testMain(['arg1','arg2'])
bluishsky 2004-04-14
  • 打赏
  • 举报
回复
谢谢chena224503(小武) ,让我弄懂了这个语句的原理。但我依然没能搞懂如何在Idle中测试我的模块,谁能在Idle中作此测试,然后把正确的操作方法详细写出来。
chena224503 2004-04-14
  • 打赏
  • 举报
回复
http://cn.diveintopython.org/odbchelper_testing.html
bluishsky 2004-04-14
  • 打赏
  • 举报
回复
对不起,duracell() ,我没能完全理解您的意思。
你是不是说在Idle的Python Shell里命令行作如下操作:

>>> python testMain arg1 arg2 ....

我试了,出错信息如下:
>>> Python

Traceback (most recent call last):
File "<pyshell#44>", line 1, in -toplevel-
Python
NameError: name 'Python' is not defined
>>> python

Traceback (most recent call last):
File "<pyshell#45>", line 1, in -toplevel-
python
NameError: name 'python' is not defined
>>>

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧