小编KeJ*_*eJi的帖子

我可以使用什么代码来检查Python是否在IDLE中运行?

正如标题所说.我想编写一个行为不同的脚本,具体取决于它是在控制台窗口内运行还是在IDLE中运行.是否存在仅在IDLE中运行时才能检查的对象?环境变量?

我在Windows上使用Python 2.6.5和2.7.

编辑:

到目前为止给出的答案有效.但我正在寻找一种正式的方法,或者看起来不像黑客.如果有人想出一个,我会接受这个答案.否则,在几天之内,我会接受最早的答案.感谢大家!

python python-idle

11
推荐指数
3
解决办法
4203
查看次数

如何从sitecustomize.py获取脚本的名称/文件?

当我运行任何Python脚本时,我希望看到脚本的文件名出现在Windows命令行窗口的标题栏中.例如,如果我运行一个名为" mytest.py" 的脚本,我想mytest在标题栏中看到" ".我希望这是自动的,所以我不必为我的每个脚本添加代码.

目前我正在尝试这样做sitecustomize.py,因为在运行Python时,包括双击Python脚本,sitecustomize在脚本运行之前导入.

我试图得到__main____file__sys.argv,但sitecustomize没有看到任何:

档案sitecustomize.py:

import __main__, sys
print "hasattr __main__.__file__:", hasattr(__main__, "__file__")
print "hasattr sys.argv:", hasattr(sys, "argv")
print "-" * 60
Run Code Online (Sandbox Code Playgroud)

档案mytest.py:

import sys
print "__file__ is:", __file__
print "sys.argv is:", sys.argv
raw_input() # don't end the script immediately
Run Code Online (Sandbox Code Playgroud)

输出:

hasattr __main__.__file__: False
hasattr sys.argv: False
------------------------------------------------------------
__file__ is: C:\Documents and Settings\Owner\Desktop\mytest.py
sys.argv is: ['C:\\Documents and Settings\\Owner\\Desktop\\mytest.py']
Run Code Online (Sandbox Code Playgroud)

python python-3.x

7
推荐指数
2
解决办法
638
查看次数

标签 统计

python ×2

python-3.x ×1

python-idle ×1