2005年07月08日 星期五 22:30
在linux下命令行直接python test.py 正常 我在leo中 run script也出错 这个可能ide运行脚本和直接命令行运行有差异的 在这儿 http://wingware.com/doc/debug/debugger-limitations 找到这样一句 Your source files must be stored on disk and accessible to the IDE. If you are trying to debug code fragments, try writing them to disk temporarily and setting the __file__ variable in the module name space before invoking Python's exec or eval 开始怀疑idle中运行调用的是用exec或eval的原因 但我写了这个代码: #test1.py print __name__ print __file__ 在activepathon中用 run和命令行都会输出"__main__",却会在__file__出错(没定义) 难到__file__定义有平台相关性? 但在window下先运行python 用 import test1 dir(test1) 会显示有__file__ 有人关心过吗,这样的结果我也好奇了 在 2005年7月8日 星期五 20:17,amingsc 写道: > Simple,您好! > > 不会吧,怎么你的结果跟我不一样呢,我是在idle中编辑运行的啊,我的是py2.4-windows. > 实在是没有 '__file__'这一项的 > > ======= 2005-07-07 20:02:00 您在来信中写道:======= > > >直接用python test.py > >我的输出是这样的 > > > >data > >['__builtins__', '__doc__', '__file__', '__name__', 'os'] > > > >不知道你的运行方式是怎样的. > >使用了某个ide的原因吗? > >不懂了 > > > >在 2005年7月7日 星期四 19:20,amingsc 写道: > > > >> Qiangning Hong,您好! > >> 代码是: > >> #-------------------test.py > >> import os.path > >> > >> print os.path.join(os.path.dirname(__file__), 'data') #error here > >> print dir() > >> # 注释掉第二句语句后运行显示dir()的结果是:['__builtins__', '__doc__', '__name__'] > >> # 怎么会没有'__file__'呢?这也是预定义变量啊 > >> > >> ======= 2005-07-07 12:27:00 您在来信中写道:======= > >> > >> >我还没有碰到__file__没有定义的情况。 > >> >你能把你出错的代码贴出来吗?你精简一下,只要能出__file__没有定义的错就行了。 > >> > > >> >On 7/7/05, amingsc <amingsc at 163.com> wrote: > >> >> Qiangning Hong,您好! > >> >> > >> >> 谢谢了啊,你的回复确实解决了我的问题,其他人都误解了我的问题了可能是我写得不够清楚吧. > >> >> 把相对路径转化成绝对路径就搞定了---想来也简单,我怎么就这么笨想不到呢 > >> >> > >> >> 但是我又发现了问题: > >> >> 运行的时候会出错:__file__没有定义 > >> >> 我查看了py文档,明明说了__name__,__doc__,__file__是预定义的变量 > >> >> 但是我在程序中用print dir()显示却偏偏少了__file__,你知道是什么问题吗? > >> >> > >> >> 于是我在'data'文件的相同目录下建了一个空文件null.py,通过import null.py来引入__file__: > >> >> import os.path > >> >> import null > >> >> filename = os.path.join(os.path.dirname(null.__file__), 'data') > >> >> > >> >> ======= 2005-07-06 23:58:00 您在来信中写道:======= > >> >> > >> >> >On 7/6/05, amingsc <amingsc at 163.com> wrote: > >> >> >> 在将程序组织成若干层次的时候,如果在底层需要设置文件的相对路径,则在调试的时候会有问题,我举例说明. > >> >> >> 比如程序如图组织: > >> >> >> > >> >> >> |------module1.py:main > >> >> >> | > >> >> >> |------package > >> >> >> | > >> >> >> |-------module2.py > >> >> >> | > >> >> >> |-------data > >> >> >> > >> >> >> 下面是代码文件: > >> >> > > >> >> >[...] > >> >> > > >> >> >> -----------------------module2.py: > >> >> >> filename = r'package\data' > >> >> > > >> >> >这里改成: > >> >> > > >> >> >import os.path > >> >> >filename = os.path.join(os.path.dirname(__file__), 'data') > >> >> > > >> >> >[...] > >> >> > > >> >> >> 问题:直接运行module1程序是正确的,但是我们在写程序的时候往往需要对当前的模块进行调试, > >> >> >> 所以在写module2的时候,调试模块则会出错(因为这时文件data的相对路径应该是r'data',而不是r'package\dat > >> >> >>a') > >> >> > > >> >> >[...] > >> >> > > >> >> > > >> >> >-- > >> >> >Qiangning Hong > >> >> >Get Firefox! > >> >> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> > >> >> > _______________________________________________ > >> >> >python-chinese list > >> >> >python-chinese at lists.python.cn > >> >> >http://python.cn/mailman/listinfo/python-chinese > >> >> > >> >> = = = = = = = = = = = = = = = = = = = = > >> >> > >> >> > >> >> 致 > >> >> 礼! > >> >> > >> >> > >> >> amingsc > >> >> amingsc at 163.com > >> >> 2005-07-07 > >> >> > >> >> > >> >> _______________________________________________ > >> >> python-chinese list > >> >> python-chinese at lists.python.cn > >> >> http://python.cn/mailman/listinfo/python-chinese > >> > > >> >-- > >> >Qiangning Hong > >> > > >> >I'm usually annoyed by IDEs because, for instance, they don't use VIM > >> >as an editor. Since I'm hooked to that, all IDEs I've used so far have > >> >failed to impress me. > >> > -- Sybren Stuvel @ c.l.python > >> > > >> >Get Firefox! > >> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> > >> > _______________________________________________ > >> >python-chinese list > >> >python-chinese at lists.python.cn > >> >http://python.cn/mailman/listinfo/python-chinese > >> > >> = = = = = = = = = = = = = = = = = = = = > >> > >> > >> 致 > >> 礼! > >> > >> > >> amingsc > >> amingsc at 163.com > >> 2005-07-07 > > = = = = = = = = = = = = = = = = = = = = > > > 致 > 礼! > > > amingsc > amingsc at 163.com > 2005-07-07
2005年07月08日 星期五 23:23
我仍然怀疑idle中运行出错是因为使用exec 我早先在命令行python出错是因为我的python刚好是2.2.0 查到的消息 http://mail.python.org/pipermail/python-list/2004-October/244269.html One gotcha: in Python 2.2 and earlier, __file__ is only defined for imported modules; "python foo.py" will raise a NameError. 也就是说在2.20前只有import的才有__file__属性 我安装activepython2.3.5后 命令行直接python test1.py正常 但用ide的run Traceback (most recent call last): File "C: \Python23\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript exec codeObject in __main__.__dict__ #####注意这一行 File "C:\Script1.py", line 2, in ? print __file__ NameError: name '__file__' is not defined 还查到这个 http://mail.python.org/pipermail/python-list/2004-October/244294.html And even in later versions it can also be a problem for your main script if you've packaged up your script into a standalone setup (e.g., with installer/py2exe). 好象__file__,还是有诡异,研究过installer/py2exe,有点晕, 不知道有没能详解一下的 在 2005年7月8日 星期五 22:30,Simple 写道: > 在linux下命令行直接python test.py 正常 > > 我在leo中 run script也出错 > 这个可能ide运行脚本和直接命令行运行有差异的 > 在这儿 > http://wingware.com/doc/debug/debugger-limitations > 找到这样一句 > Your source files must be stored on disk and accessible to the IDE. If you > are trying to debug code fragments, try writing them to disk temporarily > and setting the __file__ variable in the module name space before invoking > Python's exec or eval > > 开始怀疑idle中运行调用的是用exec或eval的原因 > 但我写了这个代码: > > #test1.py > print __name__ > print __file__ > > 在activepathon中用 run和命令行都会输出"__main__",却会在__file__出错(没定义) > 难到__file__定义有平台相关性? > > 但在window下先运行python > 用 import test1 > dir(test1) 会显示有__file__ > > 有人关心过吗,这样的结果我也好奇了 > > 在 2005年7月8日 星期五 20:17,amingsc 写道: > > > Simple,您好! > > > > 不会吧,怎么你的结果跟我不一样呢,我是在idle中编辑运行的啊,我的是py2.4-windows. > > 实在是没有 '__file__'这一项的 > > > > ======= 2005-07-07 20:02:00 您在来信中写道:======= > > > > >直接用python test.py > > >我的输出是这样的 > > > > > >data > > >['__builtins__', '__doc__', '__file__', '__name__', 'os'] > > > > > >不知道你的运行方式是怎样的. > > >使用了某个ide的原因吗? > > >不懂了 > > > > > >在 2005年7月7日 星期四 19:20,amingsc 写道: > > > > > >> Qiangning Hong,您好! > > >> 代码是: > > >> #-------------------test.py > > >> import os.path > > >> > > >> print os.path.join(os.path.dirname(__file__), 'data') #error here > > >> print dir() > > >> # 注释掉第二句语句后运行显示dir()的结果是:['__builtins__', '__doc__', '__name__'] > > >> # 怎么会没有'__file__'呢?这也是预定义变量啊 > > >> > > >> ======= 2005-07-07 12:27:00 您在来信中写道:======= > > >> > > >> >我还没有碰到__file__没有定义的情况。 > > >> >你能把你出错的代码贴出来吗?你精简一下,只要能出__file__没有定义的错就行了。 > > >> > > > >> >On 7/7/05, amingsc <amingsc at 163.com> wrote: > > >> >> Qiangning Hong,您好! > > >> >> > > >> >> 谢谢了啊,你的回复确实解决了我的问题,其他人都误解了我的问题了可能是我写得不够清楚吧. > > >> >> 把相对路径转化成绝对路径就搞定了---想来也简单,我怎么就这么笨想不到呢 > > >> >> > > >> >> 但是我又发现了问题: > > >> >> 运行的时候会出错:__file__没有定义 > > >> >> 我查看了py文档,明明说了__name__,__doc__,__file__是预定义的变量 > > >> >> 但是我在程序中用print dir()显示却偏偏少了__file__,你知道是什么问题吗? > > >> >> > > >> >> 于是我在'data'文件的相同目录下建了一个空文件null.py,通过import null.py来引入__file__: > > >> >> import os.path > > >> >> import null > > >> >> filename = os.path.join(os.path.dirname(null.__file__), 'data') > > >> >> > > >> >> ======= 2005-07-06 23:58:00 您在来信中写道:======= > > >> >> > > >> >> >On 7/6/05, amingsc <amingsc at 163.com> wrote: > > >> >> >> 在将程序组织成若干层次的时候,如果在底层需要设置文件的相对路径,则在调试的时候会有问题,我举例说明. > > >> >> >> 比如程序如图组织: > > >> >> >> > > >> >> >> |------module1.py:main > > >> >> >> | > > >> >> >> |------package > > >> >> >> | > > >> >> >> |-------module2.py > > >> >> >> | > > >> >> >> |-------data > > >> >> >> > > >> >> >> 下面是代码文件: > > >> >> > > > >> >> >[...] > > >> >> > > > >> >> >> -----------------------module2.py: > > >> >> >> filename = r'package\data' > > >> >> > > > >> >> >这里改成: > > >> >> > > > >> >> >import os.path > > >> >> >filename = os.path.join(os.path.dirname(__file__), 'data') > > >> >> > > > >> >> >[...] > > >> >> > > > >> >> >> 问题:直接运行module1程序是正确的,但是我们在写程序的时候往往需要对当前的模块进行调试, > > >> >> >> 所以在写module2的时候,调试模块则会出错(因为这时文件data的相对路径应该是r'data',而不是r'package\d > > >> >> >>at a') > > >> >> > > > >> >> >[...] > > >> >> > > > >> >> > > > >> >> >-- > > >> >> >Qiangning Hong > > >> >> >Get Firefox! > > >> >> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> > > >> >> > _______________________________________________ > > >> >> >python-chinese list > > >> >> >python-chinese at lists.python.cn > > >> >> >http://python.cn/mailman/listinfo/python-chinese > > >> >> > > >> >> = = = = = = = = = = = = = = = = = = = = > > >> >> > > >> >> > > >> >> 致 > > >> >> 礼! > > >> >> > > >> >> > > >> >> amingsc > > >> >> amingsc at 163.com > > >> >> 2005-07-07 > > >> >> > > >> >> > > >> >> _______________________________________________ > > >> >> python-chinese list > > >> >> python-chinese at lists.python.cn > > >> >> http://python.cn/mailman/listinfo/python-chinese > > >> > > > >> >-- > > >> >Qiangning Hong > > >> > > > >> >I'm usually annoyed by IDEs because, for instance, they don't use VIM > > >> >as an editor. Since I'm hooked to that, all IDEs I've used so far > > >> > have failed to impress me. > > >> > -- Sybren Stuvel @ c.l.python > > >> > > > >> >Get Firefox! > > >> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> > > >> > _______________________________________________ > > >> >python-chinese list > > >> >python-chinese at lists.python.cn > > >> >http://python.cn/mailman/listinfo/python-chinese > > >> > > >> = = = = = = = = = = = = = = = = = = = = > > >> > > >> > > >> 致 > > >> 礼! > > >> > > >> > > >> amingsc > > >> amingsc at 163.com > > >> 2005-07-07 > > > > = = = = = = = = = = = = = = = = = = = = > > > > > > 致 > > 礼! > > > > > > amingsc > > amingsc at 163.com > > 2005-07-07
2005年07月08日 星期五 23:54
你的猜测是对的,在IDLE中确实是用exec运行模块的。 python 2.4.1的源码 Lib/idlelib/ScriptBinding.py ScriptBinding.checksyntax()函数第100行为 return compile(source, filename, "exec") Lib/idlelib/PyShell.py ModifiedInterpreter.runcode()函数712行为 exec code in self.locals 应该是self.locals里面没有__file__的定义吧。 估计py2exe对主程序模块也是采用的类似的措施,因此在主程序模块中最好不要使用__file__。 On 7/8/05, Simple <qsb at nlbmol.ibp.ac.cn> wrote: > 我仍然怀疑idle中运行出错是因为使用exec > > 我早先在命令行python出错是因为我的python刚好是2.2.0 > 查到的消息 > http://mail.python.org/pipermail/python-list/2004-October/244269.html > One gotcha: in Python 2.2 and earlier, __file__ is only defined for > imported modules; "python foo.py" will raise a NameError. > 也就是说在2.20前只有import的才有__file__属性 > > 我安装activepython2.3.5后 > 命令行直接python test1.py正常 > 但用ide的run > Traceback (most recent call last): > File "C: > \Python23\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line > 310, in RunScript > exec codeObject in __main__.__dict__ #####注意这一行 > File "C:\Script1.py", line 2, in ? > print __file__ > NameError: name '__file__' is not defined > > 还查到这个 > http://mail.python.org/pipermail/python-list/2004-October/244294.html > And even in later versions it can also be a problem for your main > script if you've packaged up your script into a standalone setup > (e.g., with installer/py2exe). > 好象__file__,还是有诡异,研究过installer/py2exe,有点晕, > 不知道有没能详解一下的 > > 在 2005年7月8日 星期五 22:30,Simple 写道: > > 在linux下命令行直接python test.py 正常 > > > > 我在leo中 run script也出错 > > 这个可能ide运行脚本和直接命令行运行有差异的 > > 在这儿 > > http://wingware.com/doc/debug/debugger-limitations > > 找到这样一句 > > Your source files must be stored on disk and accessible to the IDE. If you > > are trying to debug code fragments, try writing them to disk temporarily > > and setting the __file__ variable in the module name space before invoking > > Python's exec or eval > > > > 开始怀疑idle中运行调用的是用exec或eval的原因 > > 但我写了这个代码: > > > > #test1.py > > print __name__ > > print __file__ > > > > 在activepathon中用 run和命令行都会输出"__main__",却会在__file__出错(没定义) > > 难到__file__定义有平台相关性? > > > > 但在window下先运行python > > 用 import test1 > > dir(test1) 会显示有__file__ > > > > 有人关心过吗,这样的结果我也好奇了 > > > > 在 2005年7月8日 星期五 20:17,amingsc 写道: > > > > > Simple,您好! > > > > > > 不会吧,怎么你的结果跟我不一样呢,我是在idle中编辑运行的啊,我的是py2.4-windows. > > > 实在是没有 '__file__'这一项的 > > > > > > ======= 2005-07-07 20:02:00 您在来信中写道:======= > > > > > > >直接用python test.py > > > >我的输出是这样的 > > > > > > > >data > > > >['__builtins__', '__doc__', '__file__', '__name__', 'os'] > > > > > > > >不知道你的运行方式是怎样的. > > > >使用了某个ide的原因吗? > > > >不懂了 > > > > > > > >在 2005年7月7日 星期四 19:20,amingsc 写道: > > > > > > > >> Qiangning Hong,您好! > > > >> 代码是: > > > >> #-------------------test.py > > > >> import os.path > > > >> > > > >> print os.path.join(os.path.dirname(__file__), 'data') #error here > > > >> print dir() > > > >> # 注释掉第二句语句后运行显示dir()的结果是:['__builtins__', '__doc__', '__name__'] > > > >> # 怎么会没有'__file__'呢?这也是预定义变量啊 > > > >> > > > >> ======= 2005-07-07 12:27:00 您在来信中写道:======= > > > >> > > > >> >我还没有碰到__file__没有定义的情况。 > > > >> >你能把你出错的代码贴出来吗?你精简一下,只要能出__file__没有定义的错就行了。 > > > >> > > > > >> >On 7/7/05, amingsc <amingsc at 163.com> wrote: > > > >> >> Qiangning Hong,您好! > > > >> >> > > > >> >> 谢谢了啊,你的回复确实解决了我的问题,其他人都误解了我的问题了可能是我写得不够清楚吧. > > > >> >> 把相对路径转化成绝对路径就搞定了---想来也简单,我怎么就这么笨想不到呢 > > > >> >> > > > >> >> 但是我又发现了问题: > > > >> >> 运行的时候会出错:__file__没有定义 > > > >> >> 我查看了py文档,明明说了__name__,__doc__,__file__是预定义的变量 > > > >> >> 但是我在程序中用print dir()显示却偏偏少了__file__,你知道是什么问题吗? > > > >> >> > > > >> >> 于是我在'data'文件的相同目录下建了一个空文件null.py,通过import null.py来引入__file__: > > > >> >> import os.path > > > >> >> import null > > > >> >> filename = os.path.join(os.path.dirname(null.__file__), 'data') > > > >> >> > > > >> >> ======= 2005-07-06 23:58:00 您在来信中写道:======= > > > >> >> > > > >> >> >On 7/6/05, amingsc <amingsc at 163.com> wrote: > > > >> >> >> 在将程序组织成若干层次的时候,如果在底层需要设置文件的相对路径,则在调试的时候会有问题,我举例说明. > > > >> >> >> 比如程序如图组织: > > > >> >> >> > > > >> >> >> |------module1.py:main > > > >> >> >> | > > > >> >> >> |------package > > > >> >> >> | > > > >> >> >> |-------module2.py > > > >> >> >> | > > > >> >> >> |-------data > > > >> >> >> > > > >> >> >> 下面是代码文件: > > > >> >> > > > > >> >> >[...] > > > >> >> > > > > >> >> >> -----------------------module2.py: > > > >> >> >> filename = r'package\data' > > > >> >> > > > > >> >> >这里改成: > > > >> >> > > > > >> >> >import os.path > > > >> >> >filename = os.path.join(os.path.dirname(__file__), 'data') > > > >> >> > > > > >> >> >[...] > > > >> >> > > > > >> >> >> 问题:直接运行module1程序是正确的,但是我们在写程序的时候往往需要对当前的模块进行调试, > > > >> >> >> 所以在写module2的时候,调试模块则会出错(因为这时文件data的相对路径应该是r'data',而不是r'package\d > > > >> >> >>at a') > > > >> >> > > > > >> >> >[...] > > > >> >> > > > > >> >> > > > > >> >> >-- > > > >> >> >Qiangning Hong > > > >> >> >Get Firefox! > > > >> >> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> > > > >> >> > _______________________________________________ > > > >> >> >python-chinese list > > > >> >> >python-chinese at lists.python.cn > > > >> >> >http://python.cn/mailman/listinfo/python-chinese > > > >> >> > > > >> >> = = = = = = = = = = = = = = = = = = = = > > > >> >> > > > >> >> > > > >> >> 致 > > > >> >> 礼! > > > >> >> > > > >> >> > > > >> >> amingsc > > > >> >> amingsc at 163.com > > > >> >> 2005-07-07 > > > >> >> > > > >> >> > > > >> >> _______________________________________________ > > > >> >> python-chinese list > > > >> >> python-chinese at lists.python.cn > > > >> >> http://python.cn/mailman/listinfo/python-chinese > > > >> > > > > >> >-- > > > >> >Qiangning Hong > > > >> > > > > >> >I'm usually annoyed by IDEs because, for instance, they don't use VIM > > > >> >as an editor. Since I'm hooked to that, all IDEs I've used so far > > > >> > have failed to impress me. > > > >> > -- Sybren Stuvel @ c.l.python > > > >> > > > > >> >Get Firefox! > > > >> > <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1> > > > >> > _______________________________________________ > > > >> >python-chinese list > > > >> >python-chinese at lists.python.cn > > > >> >http://python.cn/mailman/listinfo/python-chinese > > > >> > > > >> = = = = = = = = = = = = = = = = = = = = > > > >> > > > >> > > > >> 致 > > > >> 礼! > > > >> > > > >> > > > >> amingsc > > > >> amingsc at 163.com > > > >> 2005-07-07 > > > > > > = = = = = = = = = = = = = = = = = = = = > > > > > > > > > 致 > > > 礼! > > > > > > > > > amingsc > > > amingsc at 163.com > > > 2005-07-07 > > _______________________________________________ > python-chinese list > python-chinese at lists.python.cn > http://python.cn/mailman/listinfo/python-chinese > > > -- Qiangning Hong I'm usually annoyed by IDEs because, for instance, they don't use VIM as an editor. Since I'm hooked to that, all IDEs I've used so far have failed to impress me. -- Sybren Stuvel @ c.l.python Get Firefox! <http://www.spreadfirefox.com/?q=affiliates&id=67907&t=1>
2005年07月09日 星期六 09:00
python-chinese,您好! 致 礼! 听雨小楼163 hailang_0512 at 163.com 2005-07-09
Zeuux © 2025
京ICP备05028076号