相关疑难解决方法(0)

告诉Python是否处于交互模式

在Python脚本中,有没有办法判断解释器是否处于交互模式?这将非常有用,例如,当您运行交互式Python会话并导入模块时,会执行稍微不同的代码(例如,关闭日志记录).

我已经看过python是否处于-i模式并尝试了那里的代码,但是,如果使用-i标志调用Python,则该函数仅返回true,而当用于调用交互模式的命令python没有参数时.

我的意思是这样的:

if __name__=="__main__":
    #do stuff
elif __pythonIsInteractive__:
    #do other stuff
else:
    exit()
Run Code Online (Sandbox Code Playgroud)

python interactive python-2.x python-2.5

41
推荐指数
5
解决办法
1万
查看次数

程序在IDLE中工作,但在命令行失败

我正在使用Python的ctypes库与Windows DLL进行通信.当我从IDLE,Ipython运行我的代码,或者输入交互式python解释器时,它工作正常.当我从Windows命令提示符运行相同的代码时,它崩溃了.为什么单向崩溃,一种方式成功?

这是我正在运行的代码的简化版本:

import ctypes, os, sys

print "Current directory:", os.getcwd()
print "sys.path:"
for i in sys.path:
    print i

PCO_api = ctypes.oledll.LoadLibrary("SC2_Cam")

camera_handle = ctypes.c_ulong()
print "Opening camera..."
PCO_api.PCO_OpenCamera(ctypes.byref(camera_handle), 0)
print " Camera handle:", camera_handle.value

wSensor = ctypes.c_uint16(0)
print "Setting sensor format..."
PCO_api.PCO_SetSensorFormat(camera_handle, wSensor)
PCO_api.PCO_GetSensorFormat(camera_handle, ctypes.byref(wSensor))
mode_names = {0: "standard", 1:"extended"}
print " Sensor format is", mode_names[wSensor.value]
Run Code Online (Sandbox Code Playgroud)

当我从IDLE或Ipython运行此代码时,我得到以下结果:

Current directory: C:\Users\Admin\Desktop\code
sys.path:
C:\Users\Admin\Desktop\code
C:\Python27\Lib\idlelib
C:\Windows\system32\python27.zip
C:\Python27\DLLs
C:\Python27\lib
C:\Python27\lib\plat-win
C:\Python27\lib\lib-tk
C:\Python27
C:\Python27\lib\site-packages
Opening camera...
 Camera handle: 39354336
Setting sensor format... …
Run Code Online (Sandbox Code Playgroud)

python dll ctypes python-idle

6
推荐指数
2
解决办法
2714
查看次数

标签 统计

python ×2

ctypes ×1

dll ×1

interactive ×1

python-2.5 ×1

python-2.x ×1

python-idle ×1